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 == 0) {
  1395. /* First delete the existing shared config entry. This
  1396. * will allow the entry to be updated for things like
  1397. * port number changes, etc. */
  1398. slapi_delete_internal_set_pb(pb, config_entry->shared_cfg_dn,
  1399. NULL, NULL, getPluginID(), 0);
  1400. /* We don't care about the results */
  1401. slapi_delete_internal_pb(pb);
  1402. /* Now force the entry to be recreated */
  1403. rc = dna_update_shared_config(config_entry);
  1404. if (0 == rc) {
  1405. slapi_back_transaction_commit(dna_pb);
  1406. } else {
  1407. if (slapi_back_transaction_abort(dna_pb) != 0) {
  1408. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM, "dna_update_config_event: failed to abort transaction!\n");
  1409. }
  1410. }
  1411. slapi_pblock_destroy(dna_pb);
  1412. slapi_pblock_init(pb);
  1413. } else {
  1414. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1415. "dna_update_config_event: failed to start transaction\n");
  1416. }
  1417. }
  1418. }
  1419. list = PR_NEXT_LINK(list);
  1420. }
  1421. dna_delete_config(copy);
  1422. slapi_ch_free((void **)&copy);
  1423. } else {
  1424. dna_unlock();
  1425. }
  1426. bail:
  1427. slapi_pblock_destroy(pb);
  1428. }
  1429. /****************************************************
  1430. Distributed ranges Helpers
  1431. ****************************************************/
  1432. /*
  1433. * dna_fix_maxval()
  1434. *
  1435. * Attempts to extend the range represented by
  1436. * config_entry. If skip_range_request is set,
  1437. * we will only attempt to activate the next
  1438. * range. No attempt will be made to transfer
  1439. * range from another server.
  1440. *
  1441. * The lock for configEntry should be obtained
  1442. * before calling this function.
  1443. */
  1444. static int dna_fix_maxval(struct configEntry *config_entry,
  1445. int skip_range_request)
  1446. {
  1447. PRCList *servers = NULL;
  1448. PRCList *server = NULL;
  1449. PRUint64 lower = 0;
  1450. PRUint64 upper = 0;
  1451. int ret = LDAP_OPERATIONS_ERROR;
  1452. if (config_entry == NULL) {
  1453. goto bail;
  1454. }
  1455. /* If we already have a next range we only need
  1456. * to activate it. */
  1457. if (config_entry->next_range_lower != 0) {
  1458. ret = dna_activate_next_range(config_entry);
  1459. if (ret != 0) {
  1460. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1461. "dna_fix_maxval: Unable to activate the "
  1462. "next range for range %s.\n", config_entry->dn);
  1463. }
  1464. } else if (!skip_range_request && config_entry->shared_cfg_base) {
  1465. /* Find out if there are any other servers to request
  1466. * range from. */
  1467. dna_get_shared_servers(config_entry, &servers, 0 );
  1468. if (servers) {
  1469. /* We have other servers we can try to extend
  1470. * our range from. Loop through them and
  1471. * request range until someone gives us some
  1472. * values, or we hit the end of the list. */
  1473. server = PR_LIST_HEAD(servers);
  1474. while (server != servers) {
  1475. if (((struct dnaServer *)server)->remaining == 0) {
  1476. /* This server has no values left, no need to ping it */
  1477. server = PR_NEXT_LINK(server);
  1478. } else if (dna_request_range(config_entry, (struct dnaServer *)server,
  1479. &lower, &upper) != 0) {
  1480. server = PR_NEXT_LINK(server);
  1481. } else {
  1482. /* Someone provided us with a new range. Attempt
  1483. * to update the config. */
  1484. if ((ret = dna_update_next_range(config_entry, lower, upper)) == 0) {
  1485. break;
  1486. }
  1487. server = PR_NEXT_LINK(server);
  1488. }
  1489. }
  1490. /* free the list of servers */
  1491. dna_delete_shared_servers(&servers);
  1492. }
  1493. }
  1494. bail:
  1495. return ret;
  1496. }
  1497. /* dna_notice_allocation()
  1498. *
  1499. * Called after a new value has been allocated from the range.
  1500. * This function will update the config entries and cached info
  1501. * appropriately. This includes activating the next range if
  1502. * we've exhausted the current range.
  1503. *
  1504. * The last parameter is the value that has just been allocated.
  1505. * The new parameter should be the next available value. If you
  1506. * set both of these parameters to 0, then this function will
  1507. * just check if the next range needs to be activated and update
  1508. * the config accordingly.
  1509. *
  1510. * The lock for configEntry should be obtained before calling
  1511. * this function. */
  1512. static void
  1513. dna_notice_allocation(struct configEntry *config_entry, PRUint64 new,
  1514. PRUint64 last)
  1515. {
  1516. /* update our cached config entry */
  1517. if ((new != 0) && (new <= (config_entry->maxval + config_entry->interval))) {
  1518. config_entry->nextval = new;
  1519. }
  1520. /* check if we've exhausted our active range */
  1521. if ((last == config_entry->maxval) ||
  1522. (config_entry->nextval > config_entry->maxval)) {
  1523. /* If we already have a next range set, make it the
  1524. * new active range. */
  1525. if (config_entry->next_range_lower != 0) {
  1526. /* Make the next range active */
  1527. if (dna_activate_next_range(config_entry) != 0) {
  1528. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1529. "dna_notice_allocation: Unable to activate "
  1530. "the next range for range %s.\n", config_entry->dn);
  1531. }
  1532. } else {
  1533. config_entry->remaining = 0;
  1534. /* update the shared configuration */
  1535. dna_update_shared_config(config_entry);
  1536. }
  1537. } else {
  1538. if (config_entry->next_range_lower != 0) {
  1539. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1540. config_entry->interval) + ((config_entry->next_range_upper -
  1541. config_entry->next_range_lower +1) / config_entry->interval);
  1542. } else {
  1543. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1544. config_entry->interval);
  1545. }
  1546. /* update the shared configuration */
  1547. dna_update_shared_config(config_entry);
  1548. }
  1549. return;
  1550. }
  1551. static int
  1552. dna_get_shared_servers(struct configEntry *config_entry, PRCList **servers, int get_all)
  1553. {
  1554. int ret = LDAP_SUCCESS;
  1555. Slapi_PBlock *pb = NULL;
  1556. Slapi_Entry **entries = NULL;
  1557. char *attrs[7];
  1558. /* First do a search in the shared config area for this
  1559. * range to find other servers who are managing this range. */
  1560. attrs[0] = DNA_HOSTNAME;
  1561. attrs[1] = DNA_PORTNUM;
  1562. attrs[2] = DNA_SECURE_PORTNUM;
  1563. attrs[3] = DNA_REMAINING;
  1564. attrs[4] = DNA_REMOTE_BIND_METHOD;
  1565. attrs[5] = DNA_REMOTE_CONN_PROT;
  1566. attrs[6] = NULL;
  1567. pb = slapi_pblock_new();
  1568. if (NULL == pb) {
  1569. ret = LDAP_OPERATIONS_ERROR;
  1570. goto cleanup;
  1571. }
  1572. slapi_search_internal_set_pb(pb, config_entry->shared_cfg_base,
  1573. LDAP_SCOPE_ONELEVEL, "objectclass=*",
  1574. attrs, 0, NULL,
  1575. NULL, getPluginID(), 0);
  1576. slapi_search_internal_pb(pb);
  1577. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1578. if (LDAP_SUCCESS != ret) {
  1579. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1580. "dna_get_shared_servers: search failed for shared "
  1581. "config: %s [error %d]\n", config_entry->shared_cfg_base,
  1582. ret);
  1583. goto cleanup;
  1584. }
  1585. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1586. &entries);
  1587. if (entries && entries[0]) {
  1588. Slapi_DN *cfg_sdn = NULL;
  1589. int i;
  1590. cfg_sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_dn);
  1591. /* We found some entries. Go through them and
  1592. * order them based off of remaining values. */
  1593. for (i = 0; entries[i]; i++) {
  1594. /* skip our own shared config entry, unless we want all the servers */
  1595. if (get_all || slapi_sdn_compare(cfg_sdn, slapi_entry_get_sdn(entries[i]))) {
  1596. struct dnaServer *server = NULL;
  1597. /* set up the server list entry */
  1598. server = (struct dnaServer *) slapi_ch_calloc(1,
  1599. sizeof(struct dnaServer));
  1600. server->sdn = slapi_sdn_new_dn_byval(slapi_entry_get_ndn(entries[i]));
  1601. server->host = slapi_entry_attr_get_charptr(entries[i],
  1602. DNA_HOSTNAME);
  1603. server->port = slapi_entry_attr_get_uint(entries[i], DNA_PORTNUM);
  1604. server->secureport = slapi_entry_attr_get_uint(entries[i], DNA_SECURE_PORTNUM);
  1605. server->remaining = slapi_entry_attr_get_ulonglong(entries[i],
  1606. DNA_REMAINING);
  1607. server->remote_binddn = config_entry->remote_binddn;
  1608. server->remote_bindpw = config_entry->remote_bindpw;
  1609. server->remote_bind_method = slapi_entry_attr_get_charptr(entries[i],
  1610. DNA_REMOTE_BIND_METHOD);
  1611. server->remote_conn_prot = slapi_entry_attr_get_charptr(entries[i],
  1612. DNA_REMOTE_CONN_PROT);
  1613. /* validate the entry */
  1614. if (!server->host || (server->port == 0 && server->secureport == 0))
  1615. {
  1616. /* free and skip this one */
  1617. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1618. "dna_get_shared_servers: skipping invalid "
  1619. "shared config entry (%s)\n", slapi_entry_get_dn(entries[i]));
  1620. dna_free_shared_server(&server);
  1621. continue;
  1622. }
  1623. /* see if we defined a server manually */
  1624. if(server->remote_bind_method){
  1625. char *reason = NULL;
  1626. int err = 0;
  1627. if(strcasecmp(server->remote_bind_method, DNA_METHOD_DIGESTMD5) == 0 ||
  1628. strcasecmp(server->remote_bind_method, DNA_METHOD_SIMPLE) == 0){
  1629. /* requires a DN and password */
  1630. if(!server->remote_binddn || !server->remote_bindpw){
  1631. reason = "missing bind DN and/or password.";
  1632. err = 1;
  1633. }
  1634. }
  1635. if(strcasecmp(server->remote_bind_method, DNA_METHOD_SSL) == 0){
  1636. /* requires a bind DN */
  1637. if(strcasecmp(server->remote_conn_prot, DNA_PROT_SSL) != 0 &&
  1638. strcasecmp(server->remote_conn_prot, DNA_PROT_TLS) != 0 )
  1639. {
  1640. reason = "bind method (SSL) requires either SSL or TLS connection "
  1641. "protocol.";
  1642. err = 1;
  1643. }
  1644. }
  1645. if(err){
  1646. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1647. "dna_get_shared_servers: skipping invalid "
  1648. "shared config entry (%s). Reason: %s\n",
  1649. slapi_entry_get_dn(entries[i]), reason);
  1650. dna_free_shared_server(&server);
  1651. continue;
  1652. }
  1653. /* everything is ok */
  1654. server->remote_defined = 1;
  1655. }
  1656. /* add a server entry to the list */
  1657. if (*servers == NULL) {
  1658. /* first entry */
  1659. *servers = (PRCList *) slapi_ch_calloc(1,
  1660. sizeof(struct dnaServer));
  1661. PR_INIT_CLIST(*servers);
  1662. PR_INSERT_LINK(&(server->list), *servers);
  1663. } else {
  1664. /* Find the right slot for this entry. We
  1665. * want to order the entries based off of
  1666. * the remaining number of values, highest
  1667. * to lowest. */
  1668. struct dnaServer *sitem;
  1669. PRCList* item = PR_LIST_HEAD(*servers);
  1670. int inserted = 0;
  1671. while (item != *servers) {
  1672. sitem = (struct dnaServer *)item;
  1673. if (server->remaining > sitem->remaining) {
  1674. PR_INSERT_BEFORE(&(server->list), item);
  1675. inserted = 1;
  1676. break;
  1677. }
  1678. item = PR_NEXT_LINK(item);
  1679. if (*servers == item) {
  1680. /* add to tail */
  1681. PR_INSERT_BEFORE(&(server->list), item);
  1682. inserted = 1;
  1683. break;
  1684. }
  1685. }
  1686. if(!inserted){
  1687. dna_free_shared_server(&server);
  1688. }
  1689. }
  1690. }
  1691. }
  1692. slapi_sdn_free(&cfg_sdn);
  1693. }
  1694. cleanup:
  1695. slapi_free_search_results_internal(pb);
  1696. slapi_pblock_destroy(pb);
  1697. return ret;
  1698. }
  1699. /*
  1700. * dna_request_range()
  1701. *
  1702. * Requests range extension from another server.
  1703. * Returns 0 on success and will fill in upper
  1704. * and lower. Returns non-0 on failure and will
  1705. * zero out upper and lower.
  1706. */
  1707. static int dna_request_range(struct configEntry *config_entry,
  1708. struct dnaServer *server,
  1709. PRUint64 *lower, PRUint64 *upper)
  1710. {
  1711. char *bind_dn = NULL;
  1712. char *bind_passwd = NULL;
  1713. char *bind_method = NULL;
  1714. int is_ssl = 0;
  1715. struct berval *request = NULL;
  1716. char *retoid = NULL;
  1717. struct berval *responsedata = NULL;
  1718. BerElement *respber = NULL;
  1719. LDAP *ld = NULL;
  1720. char *lower_str = NULL;
  1721. char *upper_str = NULL;
  1722. int set_extend_flag = 0;
  1723. int ret = LDAP_OPERATIONS_ERROR;
  1724. int port = 0;
  1725. int timelimit;
  1726. #if defined(USE_OPENLDAP)
  1727. struct timeval timeout;
  1728. #endif
  1729. /* See if we're allowed to send a range request now */
  1730. slapi_lock_mutex(config_entry->extend_lock);
  1731. if (config_entry->extend_in_progress) {
  1732. /* We're already processing a range extention, so bail. */
  1733. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1734. "dna_request_range: Already processing a "
  1735. "range extension request. Skipping request.\n");
  1736. slapi_unlock_mutex(config_entry->extend_lock);
  1737. goto bail;
  1738. } else {
  1739. /* Set a flag indicating that we're attempting to extend this range */
  1740. config_entry->extend_in_progress = 1;
  1741. set_extend_flag = 1;
  1742. slapi_unlock_mutex(config_entry->extend_lock);
  1743. }
  1744. /* Fetch the replication bind dn info */
  1745. if (dna_get_replica_bind_creds(config_entry->shared_cfg_base, server,
  1746. &bind_dn, &bind_passwd, &bind_method,
  1747. &is_ssl, &port) != 0) {
  1748. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1749. "dna_request_range: Unable to retrieve "
  1750. "replica bind credentials.\n");
  1751. goto bail;
  1752. }
  1753. if ((request = dna_create_range_request(config_entry->shared_cfg_base)) == NULL) {
  1754. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1755. "dna_request_range: Failed to create "
  1756. "range extension extended operation request.\n");
  1757. goto bail;
  1758. }
  1759. if ((ld = slapi_ldap_init(server->host, port, is_ssl, 0)) == NULL) {
  1760. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1761. "dna_request_range: Unable to "
  1762. "initialize LDAP session to server %s:%u.\n",
  1763. server->host, server->port);
  1764. goto bail;
  1765. }
  1766. /* Disable referrals and set timelimit and a connect timeout */
  1767. ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1768. timelimit = config_entry->timeout / 1000; /* timeout is in msec */
  1769. ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &timelimit);
  1770. #if defined(USE_OPENLDAP)
  1771. timeout.tv_sec = config_entry->timeout / 1000;
  1772. timeout.tv_usec = (config_entry->timeout % 1000) * 1000;
  1773. ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &timeout);
  1774. #else
  1775. ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, &config_entry->timeout);
  1776. #endif
  1777. /* Bind to the replica server */
  1778. ret = slapi_ldap_bind(ld, bind_dn, bind_passwd, bind_method,
  1779. NULL, NULL, NULL, NULL);
  1780. if (ret != LDAP_SUCCESS) {
  1781. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1782. "dna_request_range: Error binding "
  1783. " to replica server %s:%u. [error %d]\n",
  1784. server->host, server->port, ret);
  1785. goto bail;
  1786. }
  1787. /* Send range extension request */
  1788. ret = ldap_extended_operation_s(ld, DNA_EXTEND_EXOP_REQUEST_OID,
  1789. request, NULL, NULL, &retoid, &responsedata);
  1790. if (ret != LDAP_SUCCESS) {
  1791. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1792. "dna_request_range: Error sending "
  1793. "range extension extended operation request "
  1794. "to server %s:%u [error %d]\n", server->host,
  1795. server->port, ret);
  1796. goto bail;
  1797. }
  1798. /* Verify that the OID is correct. */
  1799. if (strcmp(retoid, DNA_EXTEND_EXOP_RESPONSE_OID) != 0) {
  1800. ret = LDAP_OPERATIONS_ERROR;
  1801. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1802. "dna_request_range: Received incorrect response OID.\n");
  1803. goto bail;
  1804. }
  1805. /* Parse response */
  1806. if (BV_HAS_DATA(responsedata)) {
  1807. respber = ber_init(responsedata);
  1808. if (ber_scanf(respber, "{aa}", &lower_str, &upper_str) == LBER_ERROR) {
  1809. ret = LDAP_PROTOCOL_ERROR;
  1810. goto bail;
  1811. }
  1812. }
  1813. /* Fill in upper and lower */
  1814. if (upper_str && lower_str) {
  1815. *upper = strtoull(upper_str, 0, 0);
  1816. *lower = strtoull(lower_str, 0, 0);
  1817. ret = 0;
  1818. } else {
  1819. ret = LDAP_OPERATIONS_ERROR;
  1820. }
  1821. bail:
  1822. if (set_extend_flag) {
  1823. slapi_lock_mutex(config_entry->extend_lock);
  1824. config_entry->extend_in_progress = 0;
  1825. slapi_unlock_mutex(config_entry->extend_lock);
  1826. }
  1827. slapi_ldap_unbind(ld);
  1828. slapi_ch_free_string(&bind_dn);
  1829. slapi_ch_free_string(&bind_passwd);
  1830. slapi_ch_free_string(&bind_method);
  1831. slapi_ch_free_string(&retoid);
  1832. slapi_ch_free_string(&lower_str);
  1833. slapi_ch_free_string(&upper_str);
  1834. ber_free(respber, 1);
  1835. ber_bvfree(request);
  1836. ber_bvfree(responsedata);
  1837. if (ret != 0) {
  1838. *upper = 0;
  1839. *lower = 0;
  1840. }
  1841. return ret;
  1842. }
  1843. static struct berval *dna_create_range_request(char *range_dn)
  1844. {
  1845. struct berval *requestdata = NULL;
  1846. struct berval shared_dn = { 0, NULL };
  1847. BerElement *ber = NULL;
  1848. shared_dn.bv_val = range_dn;
  1849. shared_dn.bv_len = strlen(shared_dn.bv_val);
  1850. if((ber = ber_alloc()) == NULL) {
  1851. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1852. "dna_create_range_request: Error "
  1853. "allocating request data.\n");
  1854. goto bail;
  1855. }
  1856. if (LBER_ERROR == (ber_printf(ber, "{o}", shared_dn.bv_val, shared_dn.bv_len))) {
  1857. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1858. "dna_create_range_request: Error "
  1859. "encoding request data.\n");
  1860. goto bail;
  1861. }
  1862. if (ber_flatten(ber, &requestdata) == -1) {
  1863. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1864. "dna_create_range_request: Error "
  1865. "encoding request data.\n");
  1866. goto bail;
  1867. }
  1868. bail:
  1869. ber_free(ber, 1);
  1870. return requestdata;
  1871. }
  1872. /****************************************************
  1873. Helpers
  1874. ****************************************************/
  1875. static char *dna_get_dn(Slapi_PBlock * pb)
  1876. {
  1877. Slapi_DN *sdn = 0;
  1878. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1879. "--> dna_get_dn\n");
  1880. if (slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn)) {
  1881. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1882. "dna_get_dn: failed to get dn of changed entry");
  1883. goto bail;
  1884. }
  1885. bail:
  1886. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1887. "<-- dna_get_dn\n");
  1888. return (char *)slapi_sdn_get_dn(sdn);
  1889. }
  1890. /* config check
  1891. matching config dn or a descendent reloads config
  1892. */
  1893. static int dna_dn_is_config(char *dn)
  1894. {
  1895. int ret = 0;
  1896. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1897. "--> dna_is_config\n");
  1898. if (slapi_dn_issuffix(dn, getPluginDN())) {
  1899. ret = 1;
  1900. }
  1901. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1902. "<-- dna_is_config\n");
  1903. return ret;
  1904. }
  1905. static int
  1906. dna_dn_is_shared_config(Slapi_PBlock *pb, char *dn)
  1907. {
  1908. struct configEntry *config_entry = NULL;
  1909. Slapi_Entry *entry = NULL;
  1910. Slapi_Attr *attr = NULL;
  1911. PRCList *list = NULL;
  1912. int ret = 0;
  1913. dna_read_lock();
  1914. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  1915. list = PR_LIST_HEAD(dna_global_config);
  1916. while (list != dna_global_config) {
  1917. config_entry = (struct configEntry *) list;
  1918. if (slapi_dn_issuffix(dn, config_entry->shared_cfg_base)) {
  1919. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &entry);
  1920. /* If the entry has the dnaHost attribute, it is a shared entry */
  1921. if (slapi_entry_attr_find(entry, DNA_HOSTNAME, &attr) == 0) {
  1922. ret = 1;
  1923. break;
  1924. }
  1925. }
  1926. list = PR_NEXT_LINK(list);
  1927. }
  1928. }
  1929. dna_unlock();
  1930. return ret;
  1931. }
  1932. #define DNA_LDAP_TAG_SK_REVERSE 0x81L
  1933. static LDAPControl *dna_build_sort_control(const char *attr)
  1934. {
  1935. LDAPControl *ctrl;
  1936. BerElement *ber;
  1937. int rc;
  1938. ber = ber_alloc();
  1939. if (NULL == ber)
  1940. return NULL;
  1941. rc = ber_printf(ber, "{{stb}}", attr, DNA_LDAP_TAG_SK_REVERSE, 1);
  1942. if (-1 == rc) {
  1943. ber_free(ber, 1);
  1944. return NULL;
  1945. }
  1946. rc = slapi_build_control(LDAP_CONTROL_SORTREQUEST, ber, 1, &ctrl);
  1947. ber_free(ber, 1);
  1948. if (LDAP_SUCCESS != rc)
  1949. return NULL;
  1950. return ctrl;
  1951. }
  1952. /****************************************************
  1953. Functions that actually do things other
  1954. than config and startup
  1955. ****************************************************/
  1956. /*
  1957. * dna_first_free_value()
  1958. *
  1959. * We do search all values between nextval and maxval asking the
  1960. * server to sort them, then we check the first free spot and
  1961. * use it as newval. If we go past the end of the range, we
  1962. * return LDAP_OPERATIONS_ERROR and set newval to be > the
  1963. * maximum configured value for this range. */
  1964. static int
  1965. dna_first_free_value(struct configEntry *config_entry,
  1966. PRUint64 *newval)
  1967. {
  1968. Slapi_Entry **entries = NULL;
  1969. Slapi_PBlock *pb = NULL;
  1970. LDAPControl **ctrls = NULL;
  1971. char *filter = NULL;
  1972. char *prefix = NULL;
  1973. int multitype = 0;
  1974. int result, status;
  1975. PRUint64 tmpval, sval, i;
  1976. char *strval = NULL;
  1977. /* check if the config is already out of range */
  1978. if (config_entry->nextval > config_entry->maxval) {
  1979. *newval = config_entry->nextval;
  1980. return LDAP_OPERATIONS_ERROR;
  1981. }
  1982. prefix = config_entry->prefix;
  1983. tmpval = config_entry->nextval;
  1984. if (dna_is_multitype_range(config_entry)) {
  1985. multitype = 1;
  1986. }
  1987. /* We don't sort if we're using a prefix (non integer type) or if this
  1988. * is a multi-type range. Instead, we just search to see if the next
  1989. * value is free, and keep incrementing until we find the next free value. */
  1990. if (prefix || multitype) {
  1991. /* This will allocate the filter string for us. */
  1992. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1993. } else {
  1994. /* This is a single-type range, so just use the first (only)
  1995. * type from the list. */
  1996. ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl *));
  1997. if (NULL == ctrls)
  1998. return LDAP_OPERATIONS_ERROR;
  1999. ctrls[0] = dna_build_sort_control(config_entry->types[0]);
  2000. if (NULL == ctrls[0]) {
  2001. slapi_ch_free((void **)&ctrls);
  2002. return LDAP_OPERATIONS_ERROR;
  2003. }
  2004. filter = slapi_ch_smprintf("(&%s(&(%s>=%" NSPRIu64 ")(%s<=%" NSPRIu64 ")))",
  2005. config_entry->filter,
  2006. config_entry->types[0], tmpval,
  2007. config_entry->types[0], config_entry->maxval);
  2008. }
  2009. if (NULL == filter) {
  2010. ldap_controls_free(ctrls);
  2011. ctrls = NULL;
  2012. return LDAP_OPERATIONS_ERROR;
  2013. }
  2014. pb = slapi_pblock_new();
  2015. if (NULL == pb) {
  2016. ldap_controls_free(ctrls);
  2017. ctrls = NULL;
  2018. slapi_ch_free_string(&filter);
  2019. return LDAP_OPERATIONS_ERROR;
  2020. }
  2021. slapi_search_internal_set_pb(pb, config_entry->scope,
  2022. LDAP_SCOPE_SUBTREE, filter,
  2023. config_entry->types, 0, ctrls,
  2024. NULL, getPluginID(), 0);
  2025. slapi_search_internal_pb(pb);
  2026. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2027. if (LDAP_SUCCESS != result) {
  2028. status = LDAP_OPERATIONS_ERROR;
  2029. goto cleanup;
  2030. }
  2031. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2032. &entries);
  2033. if (NULL == entries || NULL == entries[0]) {
  2034. /* no values means we already have a good value */
  2035. *newval = tmpval;
  2036. status = LDAP_SUCCESS;
  2037. goto cleanup;
  2038. }
  2039. if (prefix || multitype) {
  2040. /* The next value identified in the config entry has already
  2041. * been taken. We just iterate through the values until we
  2042. * (hopefully) find a free one. */
  2043. for (tmpval += config_entry->interval; tmpval <= config_entry->maxval;
  2044. tmpval += config_entry->interval) {
  2045. /* This will reuse the old memory for the previous filter. It is
  2046. * guaranteed to have enough space since the filter is the same
  2047. * aside from the assertion value (we allocated enough for the
  2048. * largest supported integer). */
  2049. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  2050. /* clear out the pblock so we can re-use it */
  2051. slapi_free_search_results_internal(pb);
  2052. slapi_pblock_init(pb);
  2053. slapi_search_internal_set_pb(pb, config_entry->scope,
  2054. LDAP_SCOPE_SUBTREE, filter,
  2055. config_entry->types, 0, 0,
  2056. NULL, getPluginID(), 0);
  2057. slapi_search_internal_pb(pb);
  2058. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2059. if (LDAP_SUCCESS != result) {
  2060. status = LDAP_OPERATIONS_ERROR;
  2061. goto cleanup;
  2062. }
  2063. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2064. &entries);
  2065. if (NULL == entries || NULL == entries[0]) {
  2066. /* no values means we already have a good value */
  2067. *newval = tmpval;
  2068. status = LDAP_SUCCESS;
  2069. goto cleanup;
  2070. }
  2071. }
  2072. } else {
  2073. /* Entries are sorted and filtered for value >= tval therefore if the
  2074. * first one does not match tval it means that the value is free,
  2075. * otherwise we need to cycle through values until we find a mismatch,
  2076. * the first mismatch is the first free pit. This is guaranteed to
  2077. * be a single-type range, so we can just use the first (only)
  2078. * type from the list of types directly. */
  2079. sval = 0;
  2080. for (i = 0; NULL != entries[i]; i++) {
  2081. strval = slapi_entry_attr_get_charptr(entries[i], config_entry->types[0]);
  2082. errno = 0;
  2083. sval = strtoull(strval, 0, 0);
  2084. if (errno) {
  2085. /* something very wrong here ... */
  2086. status = LDAP_OPERATIONS_ERROR;
  2087. goto cleanup;
  2088. }
  2089. slapi_ch_free_string(&strval);
  2090. if (tmpval != sval)
  2091. break;
  2092. if (config_entry->maxval < sval)
  2093. break;
  2094. tmpval += config_entry->interval;
  2095. }
  2096. }
  2097. /* check if we went past the end of the range */
  2098. if (tmpval <= config_entry->maxval) {
  2099. *newval = tmpval;
  2100. status = LDAP_SUCCESS;
  2101. } else {
  2102. /* we set newval past the end of the range
  2103. * so the caller can easily detect that we
  2104. * overflowed the configured range. */
  2105. *newval = tmpval;
  2106. status = LDAP_OPERATIONS_ERROR;
  2107. }
  2108. cleanup:
  2109. slapi_ch_free_string(&filter);
  2110. slapi_ch_free_string(&strval);
  2111. slapi_free_search_results_internal(pb);
  2112. slapi_pblock_destroy(pb);
  2113. return status;
  2114. }
  2115. /*
  2116. * Perform ldap operationally atomic increment
  2117. * Return the next value to be assigned
  2118. */
  2119. static int dna_get_next_value(struct configEntry *config_entry,
  2120. char **next_value_ret)
  2121. {
  2122. Slapi_PBlock *pb = NULL;
  2123. LDAPMod mod_replace;
  2124. LDAPMod *mods[2];
  2125. char *replace_val[2];
  2126. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2127. char next_value[17];
  2128. PRUint64 setval = 0;
  2129. PRUint64 nextval = 0;
  2130. int ret;
  2131. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2132. "--> dna_get_next_value\n");
  2133. /* get the lock to prevent contention with other threads over
  2134. * the next new value for this range. */
  2135. slapi_lock_mutex(config_entry->lock);
  2136. /* get the first value */
  2137. ret = dna_first_free_value(config_entry, &setval);
  2138. if (LDAP_SUCCESS != ret) {
  2139. /* check if we overflowed the configured range */
  2140. if (setval > config_entry->maxval) {
  2141. /* This should not happen, as pre_op should of allocated the next range.
  2142. * In case this does occur, we will attempt to activate the next range if
  2143. * one is available. We tell dna_fix_maxval() to skip sending a range
  2144. * transfer request, we we don't want to perform any network operations
  2145. * while within a transaction. */
  2146. ret = dna_fix_maxval(config_entry, 1 /* skip range transfer request */ );
  2147. if (LDAP_SUCCESS != ret) {
  2148. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2149. "dna_get_next_value: no more values available!!\n");
  2150. goto done;
  2151. }
  2152. /* get the first value from our newly extended range */
  2153. ret = dna_first_free_value(config_entry, &setval);
  2154. if (LDAP_SUCCESS != ret)
  2155. goto done;
  2156. } else {
  2157. /* dna_first_free_value() failed for some unknown reason */
  2158. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2159. "dna_get_next_value: failed to allocate a new ID!!\n");
  2160. goto done;
  2161. }
  2162. }
  2163. nextval = setval + config_entry->interval;
  2164. /* update nextval if we have not reached the end
  2165. * of our current range */
  2166. if ((config_entry->maxval == -1) ||
  2167. (nextval <= (config_entry->maxval + config_entry->interval))) {
  2168. /* try to set the new next value in the config entry */
  2169. PR_snprintf(next_value, sizeof(next_value),"%" NSPRIu64, nextval);
  2170. /* set up our replace modify operation */
  2171. replace_val[0] = next_value;
  2172. replace_val[1] = 0;
  2173. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2174. mod_replace.mod_type = DNA_NEXTVAL;
  2175. mod_replace.mod_values = replace_val;
  2176. mods[0] = &mod_replace;
  2177. mods[1] = 0;
  2178. pb = slapi_pblock_new();
  2179. if (NULL == pb) {
  2180. ret = LDAP_OPERATIONS_ERROR;
  2181. goto done;
  2182. }
  2183. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2184. mods, 0, 0, getPluginID(), 0);
  2185. slapi_modify_internal_pb(pb);
  2186. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2187. }
  2188. if (LDAP_SUCCESS == ret) {
  2189. slapi_ch_free_string(next_value_ret);
  2190. *next_value_ret = slapi_ch_smprintf("%" NSPRIu64, setval);
  2191. if (NULL == *next_value_ret) {
  2192. ret = LDAP_OPERATIONS_ERROR;
  2193. goto done;
  2194. }
  2195. /* update our cached config */
  2196. dna_notice_allocation(config_entry, nextval, setval);
  2197. }
  2198. done:
  2199. slapi_unlock_mutex(config_entry->lock);
  2200. if (pb) {
  2201. slapi_pblock_destroy(pb);
  2202. }
  2203. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2204. "<-- dna_get_next_value\n");
  2205. return ret;
  2206. }
  2207. /*
  2208. * Get a value from the global server list. The dna_server_read_lock()
  2209. * should be held prior to calling this function.
  2210. */
  2211. static int
  2212. dna_get_shared_config_attr_val(struct configEntry *config_entry, char *attr, char *value)
  2213. {
  2214. struct dnaServer *server = NULL;
  2215. Slapi_DN *server_sdn = NULL;
  2216. int found = 0;
  2217. server_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_dn);
  2218. if (dna_global_servers) {
  2219. server = dna_global_servers;
  2220. while (server) {
  2221. if(slapi_sdn_compare(server->sdn, server_sdn) == 0){
  2222. if(strcmp(attr, DNA_REMOTE_BIND_METHOD) == 0){
  2223. if (server->remote_bind_method) {
  2224. PR_snprintf(value, DNA_REMOTE_BUFSIZ, "%s", server->remote_bind_method);
  2225. found = 1;
  2226. }
  2227. break;
  2228. } else if(strcmp(attr, DNA_REMOTE_CONN_PROT) == 0){
  2229. if (server->remote_conn_prot) {
  2230. PR_snprintf(value, DNA_REMOTE_BUFSIZ, "%s", server->remote_conn_prot);
  2231. found = 1;
  2232. }
  2233. break;
  2234. }
  2235. }
  2236. server = server->next;
  2237. }
  2238. }
  2239. slapi_sdn_free(&server_sdn);
  2240. return found;
  2241. }
  2242. /*
  2243. * dna_update_shared_config()
  2244. *
  2245. * Updates the shared config entry if one is
  2246. * configured. Returns the LDAP result code.
  2247. *
  2248. * The lock for configEntry should be obtained
  2249. * before calling this function.
  2250. * */
  2251. static int
  2252. dna_update_shared_config(struct configEntry *config_entry)
  2253. {
  2254. int ret = LDAP_SUCCESS;
  2255. if (config_entry && config_entry->shared_cfg_dn) {
  2256. /* Update the shared config entry if one is configured */
  2257. Slapi_PBlock *pb = NULL;
  2258. LDAPMod mod_replace;
  2259. LDAPMod *mods[2];
  2260. char *replace_val[2];
  2261. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2262. char remaining_vals[17];
  2263. /* We store the number of remaining assigned values
  2264. * in the shared config entry. */
  2265. PR_snprintf(remaining_vals, sizeof(remaining_vals),"%" NSPRIu64,
  2266. config_entry->remaining);
  2267. /* set up our replace modify operation */
  2268. replace_val[0] = remaining_vals;
  2269. replace_val[1] = 0;
  2270. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2271. mod_replace.mod_type = DNA_REMAINING;
  2272. mod_replace.mod_values = replace_val;
  2273. mods[0] = &mod_replace;
  2274. mods[1] = 0;
  2275. pb = slapi_pblock_new();
  2276. if (NULL == pb) {
  2277. ret = LDAP_OPERATIONS_ERROR;
  2278. } else {
  2279. slapi_modify_internal_set_pb(pb, config_entry->shared_cfg_dn,
  2280. mods, NULL, NULL, getPluginID(), 0);
  2281. slapi_modify_internal_pb(pb);
  2282. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2283. /* If the shared config for this instance doesn't
  2284. * already exist, we add it. */
  2285. if (ret == LDAP_NO_SUCH_OBJECT) {
  2286. Slapi_Entry *e = NULL;
  2287. Slapi_DN *sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_dn);
  2288. char bind_meth[DNA_REMOTE_BUFSIZ];
  2289. char conn_prot[DNA_REMOTE_BUFSIZ];
  2290. /* Set up the new shared config entry */
  2291. e = slapi_entry_alloc();
  2292. /* the entry now owns the dup'd dn */
  2293. slapi_entry_init_ext(e, sdn, NULL); /* sdn is copied into e */
  2294. slapi_sdn_free(&sdn);
  2295. slapi_entry_add_string(e, SLAPI_ATTR_OBJECTCLASS, DNA_SHAREDCONFIG);
  2296. slapi_entry_add_string(e, DNA_HOSTNAME, hostname);
  2297. slapi_entry_add_string(e, DNA_PORTNUM, portnum);
  2298. if (secureportnum) {
  2299. slapi_entry_add_string(e, DNA_SECURE_PORTNUM, secureportnum);
  2300. }
  2301. slapi_entry_add_string(e, DNA_REMAINING, remaining_vals);
  2302. /* Grab the remote server settings */
  2303. dna_server_read_lock();
  2304. if(dna_get_shared_config_attr_val(config_entry, DNA_REMOTE_BIND_METHOD, bind_meth)) {
  2305. slapi_entry_add_string(e, DNA_REMOTE_BIND_METHOD, bind_meth);
  2306. }
  2307. if(dna_get_shared_config_attr_val(config_entry, DNA_REMOTE_CONN_PROT, conn_prot)){
  2308. slapi_entry_add_string(e, DNA_REMOTE_CONN_PROT,conn_prot);
  2309. }
  2310. dna_server_unlock();
  2311. /* clear pb for re-use */
  2312. slapi_pblock_init(pb);
  2313. /* e will be consumed by slapi_add_internal() */
  2314. slapi_add_entry_internal_set_pb(pb, e, NULL, getPluginID(), 0);
  2315. slapi_add_internal_pb(pb);
  2316. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2317. }
  2318. if (ret != LDAP_SUCCESS) {
  2319. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2320. "dna_update_shared_config: Unable to update shared config entry: %s [error %d]\n",
  2321. config_entry->shared_cfg_dn, ret);
  2322. }
  2323. slapi_pblock_destroy(pb);
  2324. pb = NULL;
  2325. }
  2326. }
  2327. return ret;
  2328. }
  2329. /*
  2330. * dna_update_next_range()
  2331. *
  2332. * Sets the proper value for the next range in
  2333. * all configuration entries and in-memory cache.
  2334. *
  2335. * The range you are updating should be locked
  2336. * before calling this function.
  2337. */
  2338. static int
  2339. dna_update_next_range(struct configEntry *config_entry,
  2340. PRUint64 lower, PRUint64 upper)
  2341. {
  2342. Slapi_PBlock *pb = NULL;
  2343. LDAPMod mod_replace;
  2344. LDAPMod *mods[2];
  2345. char *replace_val[2];
  2346. /* 32 for the two numbers, 1 for the '-', and one for the '\0' */
  2347. char nextrange_value[34];
  2348. int ret = 0;
  2349. /* Try to set the new next range in the config entry. */
  2350. PR_snprintf(nextrange_value, sizeof(nextrange_value), "%" NSPRIu64 "-%" NSPRIu64,
  2351. lower, upper);
  2352. /* set up our replace modify operation */
  2353. replace_val[0] = nextrange_value;
  2354. replace_val[1] = 0;
  2355. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2356. mod_replace.mod_type = DNA_NEXT_RANGE;
  2357. mod_replace.mod_values = replace_val;
  2358. mods[0] = &mod_replace;
  2359. mods[1] = 0;
  2360. pb = slapi_pblock_new();
  2361. if (NULL == pb) {
  2362. ret = LDAP_OPERATIONS_ERROR;
  2363. goto bail;
  2364. }
  2365. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2366. mods, 0, 0, getPluginID(), 0);
  2367. slapi_modify_internal_pb(pb);
  2368. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2369. slapi_pblock_destroy(pb);
  2370. pb = NULL;
  2371. if (ret != LDAP_SUCCESS) {
  2372. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2373. "dna_update_next_range: Error updating "
  2374. "configuration entry [err=%d]\n", ret);
  2375. } else {
  2376. /* update the cached config and the shared config */
  2377. config_entry->next_range_lower = lower;
  2378. config_entry->next_range_upper = upper;
  2379. dna_notice_allocation(config_entry, 0, 0);
  2380. }
  2381. bail:
  2382. return ret;
  2383. }
  2384. /* dna_activate_next_range()
  2385. *
  2386. * Makes the next range the active range. This
  2387. * will update the config entry, the in-memory
  2388. * config info, and the shared config entry.
  2389. *
  2390. * The lock for configEntry should
  2391. * be obtained before calling this function.
  2392. */
  2393. static int
  2394. dna_activate_next_range(struct configEntry *config_entry)
  2395. {
  2396. Slapi_PBlock *pb = NULL;
  2397. LDAPMod mod_maxval;
  2398. LDAPMod mod_nextval;
  2399. LDAPMod mod_nextrange;
  2400. LDAPMod *mods[4];
  2401. char *maxval_vals[2];
  2402. char *nextval_vals[2];
  2403. char *nextrange_vals[1];
  2404. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2405. char maxval_val[17];
  2406. char nextval_val[17];
  2407. int ret = 0;
  2408. /* Setup the modify operation for the config entry */
  2409. PR_snprintf(maxval_val, sizeof(maxval_val),"%" NSPRIu64, config_entry->next_range_upper);
  2410. PR_snprintf(nextval_val, sizeof(nextval_val),"%" NSPRIu64, config_entry->next_range_lower);
  2411. maxval_vals[0] = maxval_val;
  2412. maxval_vals[1] = 0;
  2413. nextval_vals[0] = nextval_val;
  2414. nextval_vals[1] = 0;
  2415. nextrange_vals[0] = 0;
  2416. mod_maxval.mod_op = LDAP_MOD_REPLACE;
  2417. mod_maxval.mod_type = DNA_MAXVAL;
  2418. mod_maxval.mod_values = maxval_vals;
  2419. mod_nextval.mod_op = LDAP_MOD_REPLACE;
  2420. mod_nextval.mod_type = DNA_NEXTVAL;
  2421. mod_nextval.mod_values = nextval_vals;
  2422. mod_nextrange.mod_op = LDAP_MOD_DELETE;
  2423. mod_nextrange.mod_type = DNA_NEXT_RANGE;
  2424. mod_nextrange.mod_values = nextrange_vals;
  2425. mods[0] = &mod_maxval;
  2426. mods[1] = &mod_nextval;
  2427. mods[2] = &mod_nextrange;
  2428. mods[3] = 0;
  2429. /* Update the config entry first */
  2430. pb = slapi_pblock_new();
  2431. if (NULL == pb) {
  2432. ret = LDAP_OPERATIONS_ERROR;
  2433. goto bail;
  2434. }
  2435. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2436. mods, 0, 0, getPluginID(), 0);
  2437. slapi_modify_internal_pb(pb);
  2438. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2439. slapi_pblock_destroy(pb);
  2440. pb = NULL;
  2441. if (ret != LDAP_SUCCESS) {
  2442. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2443. "dna_activate_next_range: Error updating "
  2444. "configuration entry [err=%d]\n", ret);
  2445. } else {
  2446. /* Update the in-memory config info */
  2447. config_entry->maxval = config_entry->next_range_upper;
  2448. config_entry->nextval = config_entry->next_range_lower;
  2449. config_entry->next_range_upper = 0;
  2450. config_entry->next_range_lower = 0;
  2451. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  2452. config_entry->interval);
  2453. /* update the shared configuration */
  2454. dna_update_shared_config(config_entry);
  2455. }
  2456. bail:
  2457. return ret;
  2458. }
  2459. /*
  2460. * dna_is_replica_bind_dn()
  2461. *
  2462. * Checks if the passed in DN is the replica bind DN. This
  2463. * is used to check if a user is allowed to request range
  2464. * from us.
  2465. *
  2466. * Returns 1 if bind_dn matches the replica bind dn, 0 otherwise. */
  2467. static int dna_is_replica_bind_dn(char *range_dn, char *bind_dn)
  2468. {
  2469. char *replica_dn = NULL;
  2470. Slapi_DN *replica_sdn = NULL;
  2471. Slapi_DN *range_sdn = NULL;
  2472. Slapi_Entry *e = NULL;
  2473. char *attrs[3];
  2474. Slapi_Backend *be = NULL;
  2475. const char *be_suffix = NULL;
  2476. int ret = 0;
  2477. /* Find the backend suffix where the shared config is stored. */
  2478. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2479. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2480. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2481. }
  2482. /* Fetch the "cn=replica" entry for the backend that stores
  2483. * the shared config. We need to see what the configured
  2484. * replica bind DN is or if a bind DN group is defined. */
  2485. if (be_suffix) {
  2486. /* This function converts the old DN style to the new one. */
  2487. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2488. if (NULL == replica_dn) {
  2489. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2490. "dna_is_replica_bind_dn: failed to create "
  2491. "replica dn for %s\n", be_suffix);
  2492. ret = 1;
  2493. goto done;
  2494. }
  2495. replica_sdn = slapi_sdn_new_normdn_passin(replica_dn);
  2496. attrs[0] = DNA_REPL_BIND_DN;
  2497. attrs[1] = DNA_REPL_BIND_DNGROUP;
  2498. attrs[2] = 0;
  2499. /* Find cn=replica entry via search */
  2500. slapi_search_internal_get_entry(replica_sdn, attrs, &e, getPluginID());
  2501. if (e) {
  2502. /* Check if the passed in bind dn matches any of the replica bind dns. */
  2503. Slapi_Value *bind_dn_sv = slapi_value_new_string(bind_dn);
  2504. ret = slapi_entry_attr_has_syntax_value(e, DNA_REPL_BIND_DN, bind_dn_sv);
  2505. if (ret == 0) {
  2506. /* check if binddn is member of binddn group */
  2507. int i = 0;
  2508. Slapi_DN *bind_group_sdn = NULL;
  2509. Slapi_Entry *bind_group_entry = NULL;
  2510. char **bind_group_dn = slapi_entry_attr_get_charray(e, DNA_REPL_BIND_DNGROUP);
  2511. attrs[0] = "member";
  2512. attrs[1] = "uniquemember";
  2513. attrs[2] = 0;
  2514. for (i=0; bind_group_dn != NULL && bind_group_dn[i] != NULL; i++) {
  2515. if (ret) {
  2516. /* already found a member, just free group */
  2517. slapi_ch_free_string(&bind_group_dn[i]);
  2518. continue;
  2519. }
  2520. bind_group_sdn = slapi_sdn_new_normdn_passin(bind_group_dn[i]);
  2521. slapi_search_internal_get_entry(bind_group_sdn, attrs, &bind_group_entry, getPluginID());
  2522. if (bind_group_entry) {
  2523. ret = slapi_entry_attr_has_syntax_value(bind_group_entry, "member", bind_dn_sv);
  2524. if (ret == 0) {
  2525. ret = slapi_entry_attr_has_syntax_value(bind_group_entry, "uniquemember", bind_dn_sv);
  2526. }
  2527. }
  2528. slapi_entry_free(bind_group_entry);
  2529. slapi_sdn_free(&bind_group_sdn);
  2530. }
  2531. slapi_ch_free((void **) &bind_group_dn);
  2532. }
  2533. slapi_value_free(&bind_dn_sv);
  2534. } else {
  2535. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2536. "dna_is_replica_bind_dn: Failed to fetch replica entry "
  2537. "for range %s\n", range_dn);
  2538. }
  2539. }
  2540. done:
  2541. slapi_entry_free(e);
  2542. slapi_sdn_free(&range_sdn);
  2543. slapi_sdn_free(&replica_sdn);
  2544. return ret;
  2545. }
  2546. static int dna_get_replica_bind_creds(char *range_dn, struct dnaServer *server,
  2547. char **bind_dn, char **bind_passwd,
  2548. char **bind_method, int *is_ssl, int *port)
  2549. {
  2550. Slapi_PBlock *pb = NULL;
  2551. Slapi_DN *range_sdn = NULL;
  2552. char *replica_dn = NULL;
  2553. Slapi_Backend *be = NULL;
  2554. const char *be_suffix = NULL;
  2555. char *attrs[6];
  2556. char *filter = NULL;
  2557. char *bind_cred = NULL;
  2558. char *transport = NULL;
  2559. Slapi_Entry **entries = NULL;
  2560. int ret = LDAP_OPERATIONS_ERROR;
  2561. /* Find the backend suffix where the shared config is stored. */
  2562. range_sdn = slapi_sdn_new_normdn_byref(range_dn);
  2563. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2564. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2565. }
  2566. /* Fetch the replication agreement entry */
  2567. if (be_suffix) {
  2568. /* This function converts the old DN style to the new one. */
  2569. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2570. if (NULL == replica_dn) {
  2571. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2572. "dna_get_replica_bind_creds: failed to create "
  2573. "replica dn for %s\n", be_suffix);
  2574. ret = LDAP_PARAM_ERROR;
  2575. goto bail;
  2576. }
  2577. filter = slapi_ch_smprintf("(&(nsds5ReplicaHost=%s)(|(" DNA_REPL_PORT "=%u)"
  2578. "(" DNA_REPL_PORT "=%u)))",
  2579. server->host, server->port, server->secureport);
  2580. attrs[0] = DNA_REPL_BIND_DN;
  2581. attrs[1] = DNA_REPL_CREDS;
  2582. attrs[2] = DNA_REPL_BIND_METHOD;
  2583. attrs[3] = DNA_REPL_TRANSPORT;
  2584. attrs[4] = DNA_REPL_PORT;
  2585. attrs[5] = 0;
  2586. pb = slapi_pblock_new();
  2587. if (NULL == pb) {
  2588. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2589. "dna_get_replica_bind_creds: Failed to "
  2590. "allocate pblock\n");
  2591. goto bail;
  2592. }
  2593. slapi_search_internal_set_pb(pb, replica_dn,
  2594. LDAP_SCOPE_ONELEVEL, filter,
  2595. attrs, 0, NULL, NULL, getPluginID(), 0);
  2596. slapi_search_internal_pb(pb);
  2597. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2598. if (LDAP_SUCCESS != ret) {
  2599. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2600. "dna_get_replica_bind_creds: Failed to fetch replica "
  2601. "bind credentials for range %s, server %s, port %u [error %d]\n",
  2602. range_dn, server->host,
  2603. server->port ? server->port : server->secureport, ret);
  2604. goto bail;
  2605. }
  2606. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2607. &entries);
  2608. if (NULL == entries || NULL == entries[0]) {
  2609. if(server->remote_defined){
  2610. /*
  2611. * Ok there are no replication agreements for this shared server, but we
  2612. * do have custom defined authentication settings we can use.
  2613. */
  2614. ret = dna_get_remote_config_info(server, bind_dn, bind_passwd, bind_method, is_ssl, port);
  2615. goto bail;
  2616. }
  2617. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2618. "dna_get_replica_bind_creds: Failed to fetch replication "
  2619. "agreement for range %s, server %s, port %u\n", range_dn,
  2620. server->host, server->port ? server->port : server->secureport);
  2621. ret = LDAP_OPERATIONS_ERROR;
  2622. goto bail;
  2623. }
  2624. /* Get the replication bind dn and password from the agreement. It
  2625. * is up to the caller to free these when they are finished. */
  2626. slapi_ch_free_string(bind_dn);
  2627. slapi_ch_free_string(bind_method);
  2628. *bind_dn = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_DN);
  2629. *bind_method = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_METHOD);
  2630. bind_cred = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_CREDS);
  2631. transport = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_TRANSPORT);
  2632. *port = slapi_entry_attr_get_int(entries[0], DNA_REPL_PORT);
  2633. /* Check if we should use SSL */
  2634. if (transport && (strcasecmp(transport, "SSL") == 0)) {
  2635. *is_ssl = 1;
  2636. } else if (transport && (strcasecmp(transport, "TLS") == 0)) {
  2637. *is_ssl = 2;
  2638. } else {
  2639. *is_ssl = 0;
  2640. }
  2641. /* fix up the bind method */
  2642. if ((NULL == *bind_method) || (strcasecmp(*bind_method, "SIMPLE") == 0)) {
  2643. slapi_ch_free_string(bind_method);
  2644. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2645. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2646. slapi_ch_free_string(bind_method);
  2647. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2648. } else if (strcasecmp(*bind_method, "SASL/GSSAPI") == 0) {
  2649. slapi_ch_free_string(bind_method);
  2650. *bind_method = slapi_ch_strdup("GSSAPI");
  2651. } else if (strcasecmp(*bind_method, "SASL/DIGEST-MD5") == 0) {
  2652. slapi_ch_free_string(bind_method);
  2653. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2654. } else { /* some other weird value */
  2655. ; /* just use it directly */
  2656. }
  2657. /* Decode the password */
  2658. if (bind_cred) {
  2659. int pw_ret = 0;
  2660. slapi_ch_free_string(bind_passwd);
  2661. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2662. if (pw_ret == -1) {
  2663. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2664. "dna_get_replica_bind_creds: Failed to decode "
  2665. "replica bind credentials for range %s, server %s, "
  2666. "port %u\n", range_dn, server->host, server->port);
  2667. goto bail;
  2668. } else if (pw_ret != 0) {
  2669. /* The password was already in clear text, so pw_rever_decode
  2670. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2671. * to prevent a double free. The memory is now owned by
  2672. * bind_passwd, which is the callers responsibility to free. */
  2673. bind_cred = NULL;
  2674. }
  2675. }
  2676. }
  2677. /* If we got here, we succesfully got the
  2678. * creds. Set the success return value. */
  2679. ret = 0;
  2680. bail:
  2681. slapi_ch_free_string(&transport);
  2682. slapi_ch_free_string(&filter);
  2683. slapi_sdn_free(&range_sdn);
  2684. slapi_ch_free_string(&replica_dn);
  2685. slapi_ch_free_string(&bind_cred);
  2686. slapi_free_search_results_internal(pb);
  2687. slapi_pblock_destroy(pb);
  2688. return ret;
  2689. }
  2690. static int
  2691. dna_get_remote_config_info( struct dnaServer *server, char **bind_dn, char **bind_passwd,
  2692. char **bind_method, int *is_ssl, int *port)
  2693. {
  2694. int rc = 0;
  2695. /* populate the bind info */
  2696. slapi_ch_free_string(bind_dn);
  2697. slapi_ch_free_string(bind_method);
  2698. *bind_dn = slapi_ch_strdup(server->remote_binddn);
  2699. *bind_method = slapi_ch_strdup(server->remote_bind_method);
  2700. /* fix up the bind method */
  2701. if ((NULL == *bind_method) || (strcasecmp(*bind_method, DNA_METHOD_SIMPLE) == 0)) {
  2702. slapi_ch_free_string(bind_method);
  2703. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2704. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2705. slapi_ch_free_string(bind_method);
  2706. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2707. } else if (strcasecmp(*bind_method, DNA_METHOD_GSSAPI) == 0) {
  2708. slapi_ch_free_string(bind_method);
  2709. *bind_method = slapi_ch_strdup("GSSAPI");
  2710. } else if (strcasecmp(*bind_method, DNA_METHOD_DIGESTMD5) == 0) {
  2711. slapi_ch_free_string(bind_method);
  2712. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2713. } else { /* some other weird value */
  2714. ; /* just use it directly */
  2715. }
  2716. if(server->remote_conn_prot && strcasecmp(server->remote_conn_prot, DNA_PROT_SSL) == 0){
  2717. *is_ssl = 1;
  2718. } else if(server->remote_conn_prot && strcasecmp(server->remote_conn_prot, DNA_PROT_TLS) == 0){
  2719. *is_ssl = 2;
  2720. } else {
  2721. *is_ssl = 0;
  2722. }
  2723. if(*is_ssl == 1){ /* SSL(covers TLS over ssl) */
  2724. if (server->secureport){
  2725. *port = server->secureport;
  2726. } else {
  2727. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2728. "dna_get_remote_config_info: Using SSL protocol, but the secure "
  2729. "port is not defined.\n");
  2730. return -1;
  2731. }
  2732. } else { /* LDAP/TLS(non secure port) */
  2733. if(server->port){
  2734. *port = server->port;
  2735. } else {
  2736. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2737. "dna_get_remote_config_info: Using %s protocol, but the non-secure "
  2738. "port is not defined.\n", server->remote_conn_prot);
  2739. return -1;
  2740. }
  2741. }
  2742. /* Decode the password */
  2743. if (server->remote_bindpw) {
  2744. char *bind_cred = slapi_ch_strdup(server->remote_bindpw);
  2745. int pw_ret = 0;
  2746. slapi_ch_free_string(bind_passwd);
  2747. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2748. if (pw_ret == -1) {
  2749. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2750. "dna_get_remote_config_info: Failed to decode "
  2751. "replica bind credentials for server %s, "
  2752. "port %u\n", server->host,
  2753. server->port ? server->port : server->secureport);
  2754. rc = -1;
  2755. } else if (pw_ret != 0) {
  2756. /*
  2757. * The password was already in clear text, so pw_rever_decode
  2758. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2759. * to prevent a double free. The memory is now owned by
  2760. * bind_passwd, which is the callers responsibility to free.
  2761. */
  2762. bind_cred = NULL;
  2763. }
  2764. slapi_ch_free_string(&bind_cred);
  2765. }
  2766. return rc;
  2767. }
  2768. /*
  2769. * dna_list_contains_type()
  2770. *
  2771. * Checks if a type is contained in a list of types.
  2772. * Returns 1 if the type is found, 0 otherwise.
  2773. */
  2774. static int
  2775. dna_list_contains_type(char **list, char *type)
  2776. {
  2777. int ret = 0;
  2778. int i = 0;
  2779. if (list && type) {
  2780. for (i = 0; list[i]; i++) {
  2781. if (slapi_attr_types_equivalent(type, list[i])) {
  2782. ret = 1;
  2783. break;
  2784. }
  2785. }
  2786. }
  2787. return ret;
  2788. }
  2789. /*
  2790. * dna_list_contains_types()
  2791. *
  2792. * Checks if all types in one list (types) are contained
  2793. * in another list of types (list). Returns 1 if all
  2794. * types are found, 0 otherwise.
  2795. */
  2796. static int
  2797. dna_list_contains_types(char **list, char **types)
  2798. {
  2799. int ret = 1;
  2800. int i = 0;
  2801. int j = 0;
  2802. if (list && types) {
  2803. for (i = 0; types[i]; i++) {
  2804. int found = 0;
  2805. for (j = 0; list[j]; j++) {
  2806. if (slapi_attr_types_equivalent(types[i], list[i])) {
  2807. found = 1;
  2808. break;
  2809. }
  2810. }
  2811. if (!found) {
  2812. ret = 0;
  2813. break;
  2814. }
  2815. }
  2816. } else {
  2817. ret = 0;
  2818. }
  2819. return ret;
  2820. }
  2821. /*
  2822. * dna_list_remove_type()
  2823. *
  2824. * Removes a type from a list of types.
  2825. */
  2826. static void
  2827. dna_list_remove_type(char **list, char *type)
  2828. {
  2829. int i = 0;
  2830. int found_type = 0;
  2831. if (list && type) {
  2832. /* Go through the list until we find the type that
  2833. * we want to remove. We simply free the type we
  2834. * want to remove and shift the remaining array
  2835. * elements down by one index. This will leave us
  2836. * with two NULL elements at the end of the list,
  2837. * but this should not cause any problems. */
  2838. for (i = 0; list[i]; i++) {
  2839. if (found_type) {
  2840. list[i] = list[i + 1];
  2841. } else if (slapi_attr_types_equivalent(type, list[i])) {
  2842. slapi_ch_free_string(&list[i]);
  2843. list[i] = list[i + 1];
  2844. found_type = 1;
  2845. }
  2846. }
  2847. }
  2848. }
  2849. /*
  2850. * dna_is_multitype_range()
  2851. *
  2852. * Returns 1 if the range has multiple types configured.
  2853. * Returns 0 otherwise.
  2854. */
  2855. static int dna_is_multitype_range(struct configEntry *config_entry)
  2856. {
  2857. int ret = 0;
  2858. if (config_entry && config_entry->types && config_entry->types[1]) {
  2859. ret = 1;
  2860. }
  2861. return ret;
  2862. }
  2863. /*
  2864. * dna_create_valcheck_filter()
  2865. *
  2866. * Creates a filter string used to check if a value is free. If
  2867. * filter already holds a valid pointer, it is assumed to have enough
  2868. * space to hold the filter. This allows the same memory to be used
  2869. * over and over when you only want to change the assertion value.
  2870. * If filter contains a NULL pointer, a new string of the appropriate
  2871. * size will be allocated. The caller must free this when finished.
  2872. */
  2873. static void
  2874. dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter)
  2875. {
  2876. int filterlen = 0;
  2877. int typeslen = 0;
  2878. int i = 0;
  2879. int bytes_out = 0;
  2880. int multitype = 0;
  2881. /* Just return if we didn't get an address for the filter. */
  2882. if (filter == NULL) {
  2883. return;
  2884. }
  2885. /* To determine the filter length, we add together the following:
  2886. *
  2887. * - the string length of the filter in the config
  2888. * - the string length sum of all configured types
  2889. * - 23 bytes for each type (20 for the max string
  2890. * representation of a NSPRIu64, 3 for "(=)"
  2891. * - 3 bytes for the beginning and end of the filter - "(&" and ")"
  2892. * - 3 bytes to OR together multiple types (if present) - "(|" and ")"
  2893. * - the string length of the prefix (if one is configured) for each type
  2894. * - 1 byte for the trailing \0
  2895. *
  2896. * The filter length should be the same every time if the config struct
  2897. * has not been changed. We need to calculate the filter length even
  2898. * if we are reusing memory since we have no other way of knowing the
  2899. * length used when it was originally allocated. We trust the caller
  2900. * to only reuse the pointer with the same config struct.
  2901. */
  2902. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2903. typeslen += strlen(config_entry->types[i]);
  2904. }
  2905. if (i > 1) {
  2906. multitype = 1;
  2907. }
  2908. filterlen = strlen(config_entry->filter) + typeslen +
  2909. (i * 23) + 3 + 1 +
  2910. (config_entry->prefix ? (i * strlen(config_entry->prefix)) : 0) +
  2911. (multitype ? 3 : 0);
  2912. /* Allocate space for the filter if it hasn't been allocated yet. */
  2913. if (*filter == NULL) {
  2914. *filter = slapi_ch_malloc(filterlen);
  2915. }
  2916. /* Write out the beginning of the filter. If multiple types
  2917. * are configured, we need to OR together the search clauses
  2918. * for the types. */
  2919. if (multitype) {
  2920. bytes_out = snprintf(*filter, filterlen, "(&%s(|", config_entry->filter);
  2921. } else {
  2922. bytes_out = snprintf(*filter, filterlen, "(&%s", config_entry->filter);
  2923. }
  2924. /* Loop through the types and append each filter clause. */
  2925. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2926. bytes_out += snprintf(*filter + bytes_out, filterlen - bytes_out,
  2927. "(%s=%s%" PRIu64 ")", config_entry->types[i],
  2928. config_entry->prefix ? config_entry->prefix : "",
  2929. value);
  2930. }
  2931. /* Append the end of the filter. We need an extra paren
  2932. * to close out the OR if we have multiple types. */
  2933. if (multitype) {
  2934. strncat(*filter, "))", filterlen - bytes_out);
  2935. } else {
  2936. strncat(*filter, ")", filterlen - bytes_out);
  2937. }
  2938. }
  2939. /* This function is called at BEPREOP timing to add uid/gidNumber
  2940. * if modtype is missing */
  2941. static int
  2942. _dna_pre_op_add(Slapi_PBlock *pb, Slapi_Entry *e, char **errstr)
  2943. {
  2944. int ret = DNA_SUCCESS;
  2945. PRCList *list = NULL;
  2946. struct configEntry *config_entry = NULL;
  2947. char *dn = NULL;
  2948. char *value = NULL;
  2949. char **types_to_generate = NULL;
  2950. char **generated_types = NULL;
  2951. PRUint64 setval = 0;
  2952. int i;
  2953. if (0 == (dn = dna_get_dn(pb))) {
  2954. goto bail;
  2955. }
  2956. /*
  2957. * Find the config that matches this entry, Set the types that need to be
  2958. * generated to DNA_NEEDS_UPDATE. The be_txn_preop will set the values if
  2959. * the operation hasn't been rejected by that point.
  2960. *
  2961. * We also check if we need to get the next range of values, and grab them.
  2962. * We do this here so we don't have to do it in the be_txn_preop.
  2963. */
  2964. dna_read_lock();
  2965. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2966. list = PR_LIST_HEAD(dna_global_config);
  2967. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  2968. config_entry = (struct configEntry *) list;
  2969. /* Did we already service all of these configured types? */
  2970. if (dna_list_contains_types(generated_types, config_entry->types)) {
  2971. goto next;
  2972. }
  2973. /* is the entry in scope? */
  2974. if (config_entry->scope &&
  2975. !slapi_dn_issuffix(dn, config_entry->scope)) {
  2976. goto next;
  2977. }
  2978. /* is this entry in an excluded scope? */
  2979. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  2980. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  2981. goto next;
  2982. }
  2983. }
  2984. /* does the entry match the filter? */
  2985. if (config_entry->slapi_filter) {
  2986. ret = slapi_vattr_filter_test(pb, e, config_entry->slapi_filter, 0);
  2987. if (LDAP_SUCCESS != ret) {
  2988. goto next;
  2989. }
  2990. }
  2991. if (dna_is_multitype_range(config_entry)) {
  2992. /* For a multi-type range, we only generate a value
  2993. * for types where the magic value is set. We do not
  2994. * generate a value for missing types. */
  2995. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2996. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  2997. if (value){
  2998. if(config_entry->generate == NULL || !slapi_UTF8CASECMP(config_entry->generate, value)){
  2999. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[i]));
  3000. }
  3001. slapi_ch_free_string(&value);
  3002. }
  3003. }
  3004. } else {
  3005. /* For a single type range, we generate the value if
  3006. * the magic value is set or if the type is missing. */
  3007. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  3008. if((config_entry->generate == NULL) || (0 == value) ||
  3009. (value && !slapi_UTF8CASECMP(config_entry->generate, value))){
  3010. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[0]));
  3011. }
  3012. slapi_ch_free_string(&value);
  3013. }
  3014. if (types_to_generate && types_to_generate[0]) {
  3015. /* add - add to entry */
  3016. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3017. slapi_entry_attr_set_charptr(e, types_to_generate[i],
  3018. /* no need to dup */
  3019. DNA_NEEDS_UPDATE);
  3020. }
  3021. /* Update the internalModifiersname for this add op */
  3022. add_internal_modifiersname(pb, e);
  3023. /* Make sure we don't generate for this
  3024. * type again by keeping a list of types
  3025. * we have generated for already.
  3026. */
  3027. if (generated_types == NULL) {
  3028. /* If we don't have a list of generated types yet,
  3029. * we can just use the types_to_generate list so
  3030. * we don't have to allocate anything. */
  3031. generated_types = types_to_generate;
  3032. types_to_generate = NULL;
  3033. } else {
  3034. /* Just reuse the elements out of types_to_generate for the
  3035. * generated types list to avoid allocating them again. */
  3036. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  3037. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  3038. types_to_generate[i] = NULL;
  3039. }
  3040. }
  3041. /* free up */
  3042. slapi_ch_array_free(types_to_generate);
  3043. types_to_generate = NULL;
  3044. /*
  3045. * Now grab the next value and see if we need to get the next range
  3046. */
  3047. slapi_lock_mutex(config_entry->lock);
  3048. ret = dna_first_free_value(config_entry, &setval);
  3049. if (LDAP_SUCCESS != ret) {
  3050. /* check if we overflowed the configured range */
  3051. if (setval > config_entry->maxval) {
  3052. /* try for a new range or fail */
  3053. ret = dna_fix_maxval(config_entry, 0);
  3054. if (LDAP_SUCCESS != ret) {
  3055. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3056. "dna_pre_op: no more values available!!\n");
  3057. /* Set an error string to be returned to the client. */
  3058. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3059. " %s failed! Unable to proceed.",
  3060. config_entry->dn);
  3061. slapi_unlock_mutex(config_entry->lock);
  3062. break;
  3063. }
  3064. /* Make sure dna_first_free_value() doesn't error out */
  3065. ret = dna_first_free_value(config_entry, &setval);
  3066. if (LDAP_SUCCESS != ret){
  3067. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3068. "dna_pre_op: failed to allocate a new ID 1\n");
  3069. /* Set an error string to be returned to the client. */
  3070. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3071. " %s failed! Unable to proceed.",
  3072. config_entry->dn);
  3073. slapi_unlock_mutex(config_entry->lock);
  3074. break;
  3075. }
  3076. } else {
  3077. /* dna_first_free_value() failed for some unknown reason */
  3078. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3079. "dna_pre_op: failed to allocate a new ID!! 2\n");
  3080. /* Set an error string to be returned to the client. */
  3081. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3082. " %s failed! Unable to proceed.",
  3083. config_entry->dn);
  3084. slapi_unlock_mutex(config_entry->lock);
  3085. break;
  3086. }
  3087. }
  3088. /* Check if we passed the threshold and try to fix maxval if so.
  3089. * We don't need to do this if we already have a next range on
  3090. * deck. We don't check the result of dna_fix_maxval() since
  3091. * we aren't completely out of values yet. Any failure here is
  3092. * really a soft failure. */
  3093. if ((config_entry->next_range_lower == 0) &&
  3094. (config_entry->remaining <= config_entry->threshold)) {
  3095. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3096. "dna_pre_op: Passed threshold of %"
  3097. NSPRIu64 " remaining values "
  3098. "for range %s. (%" NSPRIu64 " values remain)\n",
  3099. config_entry->threshold, config_entry->dn,
  3100. config_entry->remaining);
  3101. dna_fix_maxval(config_entry, 0);
  3102. }
  3103. slapi_unlock_mutex(config_entry->lock);
  3104. } else if (types_to_generate) {
  3105. slapi_ch_free((void **)&types_to_generate);
  3106. }
  3107. next:
  3108. ret = DNA_SUCCESS;
  3109. list = PR_NEXT_LINK(list);
  3110. }
  3111. }
  3112. dna_unlock();
  3113. slapi_ch_array_free(generated_types);
  3114. bail:
  3115. return ret;
  3116. }
  3117. /* This function is called at BEPREOP timing to add uid/gidNumber
  3118. * if modtype is missing */
  3119. static int
  3120. _dna_pre_op_modify(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Mods *smods, char **errstr)
  3121. {
  3122. int ret = DNA_SUCCESS;
  3123. PRCList *list = NULL;
  3124. struct configEntry *config_entry = NULL;
  3125. char *dn = NULL;
  3126. char *value = NULL;
  3127. Slapi_Mod *next_mod = NULL;
  3128. Slapi_Mod *smod = NULL;
  3129. Slapi_Attr *attr = NULL;
  3130. char *type = NULL;
  3131. int e_numvals = 0;
  3132. int numvals = 0;
  3133. struct berval *bv = NULL;
  3134. char **types_to_generate = NULL;
  3135. char **generated_types = NULL;
  3136. PRUint64 setval = 0;
  3137. int len = 0;
  3138. int i;
  3139. if (0 == (dn = dna_get_dn(pb))) {
  3140. goto bail;
  3141. }
  3142. /*
  3143. * Find the config that matches this entry, Set the types that need to be
  3144. * generated to DNA_NEEDS_UPDATE. The be_txn_preop will set the values if
  3145. * the operation hasn't been rejected by that point.
  3146. *
  3147. * We also check if we need to get the next range of values, and grab them.
  3148. * We do this here so we don't have to do it in the be_txn_preop.
  3149. */
  3150. dna_read_lock();
  3151. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3152. list = PR_LIST_HEAD(dna_global_config);
  3153. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  3154. config_entry = (struct configEntry *) list;
  3155. /* Did we already service all of these configured types? */
  3156. if (dna_list_contains_types(generated_types, config_entry->types)) {
  3157. goto next;
  3158. }
  3159. /* is the entry in scope? */
  3160. if (config_entry->scope &&
  3161. !slapi_dn_issuffix(dn, config_entry->scope)) {
  3162. goto next;
  3163. }
  3164. /* is this entry in an excluded scope? */
  3165. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  3166. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  3167. goto next;
  3168. }
  3169. }
  3170. /* does the entry match the filter? */
  3171. if (config_entry->slapi_filter) {
  3172. ret = slapi_vattr_filter_test(pb, e,
  3173. config_entry->slapi_filter, 0);
  3174. if (LDAP_SUCCESS != ret) {
  3175. goto next;
  3176. }
  3177. }
  3178. /* check mods for magic value */
  3179. next_mod = slapi_mod_new();
  3180. smod = slapi_mods_get_first_smod(smods, next_mod);
  3181. while (smod) {
  3182. type = (char *)slapi_mod_get_type(smod);
  3183. /* See if the type matches any configured type. */
  3184. if (dna_list_contains_type(config_entry->types, type)) {
  3185. /* If all values are being deleted, we need to
  3186. * generate a new value. We don't do this for
  3187. * multi-type ranges since they require the magic
  3188. * value to be specified to trigger generation. */
  3189. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  3190. !dna_is_multitype_range(config_entry)) {
  3191. numvals = slapi_mod_get_num_values(smod);
  3192. if (numvals == 0) {
  3193. slapi_ch_array_add(&types_to_generate,
  3194. slapi_ch_strdup(type));
  3195. } else {
  3196. e_numvals = 0;
  3197. slapi_entry_attr_find(e, type, &attr);
  3198. if (attr) {
  3199. slapi_attr_get_numvalues(attr, &e_numvals);
  3200. if (numvals >= e_numvals) {
  3201. slapi_ch_array_add(&types_to_generate,
  3202. slapi_ch_strdup(type));
  3203. }
  3204. }
  3205. }
  3206. } else {
  3207. /* This is either adding or replacing a value */
  3208. bv = slapi_mod_get_first_value(smod);
  3209. /* If this type is already in the to be generated
  3210. * list, a previous mod in this same modify operation
  3211. * either removed all values or set the magic value.
  3212. * It's possible that this mod is adding a valid value,
  3213. * which means we would not want to generate a new value.
  3214. * It is safe to remove this type from the to be
  3215. * generated list since it will be re-added here if
  3216. * necessary. */
  3217. if (dna_list_contains_type(types_to_generate, type)) {
  3218. dna_list_remove_type(types_to_generate, type);
  3219. }
  3220. /* If we have a value, see if it's the magic value. */
  3221. if (bv) {
  3222. if(config_entry->generate == NULL){
  3223. /* we don't have a magic number set, so apply the update */
  3224. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3225. } else {
  3226. len = strlen(config_entry->generate);
  3227. if (len == bv->bv_len) {
  3228. if (!slapi_UTF8NCASECMP(bv->bv_val, config_entry->generate,len)){
  3229. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3230. }
  3231. }
  3232. }
  3233. } else if (!dna_is_multitype_range(config_entry)) {
  3234. /* This is a replace with no new values,
  3235. * so we need to generate a new value if this
  3236. * is not a multi-type range. */
  3237. slapi_ch_array_add(&types_to_generate,slapi_ch_strdup(type));
  3238. }
  3239. }
  3240. }
  3241. slapi_mod_done(next_mod);
  3242. smod = slapi_mods_get_next_smod(smods, next_mod);
  3243. }
  3244. slapi_mod_free(&next_mod);
  3245. /* We need to perform one last check for modify operations. If an
  3246. * entry within the scope has not triggered generation yet, we need
  3247. * to see if a value exists for the managed type in the resulting
  3248. * entry. This will catch a modify operation that brings an entry
  3249. * into scope for a managed range, but doesn't supply a value for
  3250. * the managed type. We don't do this for multi-type ranges. */
  3251. if ((!types_to_generate ||
  3252. (types_to_generate && !types_to_generate[0])) &&
  3253. !dna_is_multitype_range(config_entry)) {
  3254. if (slapi_entry_attr_find(e, config_entry->types[0], &attr)
  3255. != 0) {
  3256. slapi_ch_array_add(&types_to_generate,
  3257. slapi_ch_strdup(config_entry->types[0]));
  3258. }
  3259. }
  3260. if (types_to_generate && types_to_generate[0]) {
  3261. /* mod - add to mods */
  3262. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3263. slapi_mods_add_string(smods, LDAP_MOD_REPLACE,
  3264. types_to_generate[i],
  3265. /* no need to dup */
  3266. DNA_NEEDS_UPDATE);
  3267. }
  3268. /* Update the internalModifersname for this mod op */
  3269. modify_update_last_modified_attr(pb, smods);
  3270. /* Make sure we don't generate for this
  3271. * type again by keeping a list of types
  3272. * we have generated for already.
  3273. */
  3274. if (generated_types == NULL) {
  3275. /* If we don't have a list of generated types yet,
  3276. * we can just use the types_to_generate list so
  3277. * we don't have to allocate anything. */
  3278. generated_types = types_to_generate;
  3279. types_to_generate = NULL;
  3280. } else {
  3281. /* Just reuse the elements out of types_to_generate for the
  3282. * generated types list to avoid allocating them again. */
  3283. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  3284. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  3285. types_to_generate[i] = NULL;
  3286. }
  3287. }
  3288. /* free up */
  3289. slapi_ch_free_string(&value);
  3290. slapi_ch_array_free(types_to_generate);
  3291. types_to_generate = NULL;
  3292. /*
  3293. * Now grab the next value and see if we need to get the next range
  3294. */
  3295. slapi_lock_mutex(config_entry->lock);
  3296. ret = dna_first_free_value(config_entry, &setval);
  3297. if (LDAP_SUCCESS != ret) {
  3298. /* check if we overflowed the configured range */
  3299. if (setval > config_entry->maxval) {
  3300. /* try for a new range or fail */
  3301. ret = dna_fix_maxval(config_entry, 0);
  3302. if (LDAP_SUCCESS != ret) {
  3303. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3304. "dna_pre_op: no more values available!!\n");
  3305. /* Set an error string to be returned to the client. */
  3306. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3307. " %s failed! Unable to proceed.",
  3308. config_entry->dn);
  3309. slapi_unlock_mutex(config_entry->lock);
  3310. break;
  3311. }
  3312. /* Make sure dna_first_free_value() doesn't error out */
  3313. ret = dna_first_free_value(config_entry, &setval);
  3314. if (LDAP_SUCCESS != ret){
  3315. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3316. "dna_pre_op: failed to allocate a new ID\n");
  3317. /* Set an error string to be returned to the client. */
  3318. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3319. " %s failed! Unable to proceed.",
  3320. config_entry->dn);
  3321. slapi_unlock_mutex(config_entry->lock);
  3322. break;
  3323. }
  3324. } else {
  3325. /* dna_first_free_value() failed for some unknown reason */
  3326. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3327. "dna_pre_op: failed to allocate a new ID!!\n");
  3328. /* Set an error string to be returned to the client. */
  3329. *errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3330. " %s failed! Unable to proceed.",
  3331. config_entry->dn);
  3332. slapi_unlock_mutex(config_entry->lock);
  3333. break;
  3334. }
  3335. }
  3336. /* Check if we passed the threshold and try to fix maxval if so.
  3337. * We don't need to do this if we already have a next range on
  3338. * deck. We don't check the result of dna_fix_maxval() since
  3339. * we aren't completely out of values yet. Any failure here is
  3340. * really a soft failure. */
  3341. if ((config_entry->next_range_lower == 0) &&
  3342. (config_entry->remaining <= config_entry->threshold)) {
  3343. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3344. "dna_pre_op: Passed threshold of %"
  3345. NSPRIu64 " remaining values "
  3346. "for range %s. (%" NSPRIu64 " values remain)\n",
  3347. config_entry->threshold, config_entry->dn,
  3348. config_entry->remaining);
  3349. dna_fix_maxval(config_entry, 0);
  3350. }
  3351. slapi_unlock_mutex(config_entry->lock);
  3352. } else if (types_to_generate) {
  3353. slapi_ch_free((void **)&types_to_generate);
  3354. }
  3355. next:
  3356. ret = 0;
  3357. list = PR_NEXT_LINK(list);
  3358. }
  3359. }
  3360. dna_unlock();
  3361. slapi_ch_array_free(generated_types);
  3362. bail:
  3363. return ret;
  3364. }
  3365. /* for mods and adds:
  3366. where dn's are supplied, the closest in scope
  3367. is used as long as the type filter matches
  3368. and the type has not been generated yet.
  3369. */
  3370. static int
  3371. dna_pre_op(Slapi_PBlock * pb, int modtype)
  3372. {
  3373. struct slapi_entry *e = NULL;
  3374. Slapi_Entry *test_e = NULL;
  3375. Slapi_Entry *resulting_e = NULL;
  3376. char *errstr = NULL;
  3377. char *dn = NULL;
  3378. Slapi_Mods *smods = NULL;
  3379. LDAPMod **mods;
  3380. int ret = 0;
  3381. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3382. "--> dna_pre_op\n");
  3383. if (0 == (dn = dna_get_dn(pb))) {
  3384. goto bail;
  3385. }
  3386. if (dna_isrepl(pb)) {
  3387. /* if repl, the dna values should be already in the entry. */
  3388. goto bail;
  3389. }
  3390. if (LDAP_CHANGETYPE_ADD == modtype) {
  3391. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  3392. if (NULL == e) {
  3393. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3394. "dna_pre_op: no add entry set for add\n");
  3395. goto bail;
  3396. }
  3397. } else {
  3398. slapi_pblock_get(pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  3399. if (NULL == e) {
  3400. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3401. "dna_pre_op: no pre op entry set for modify\n");
  3402. goto bail;
  3403. }
  3404. /* grab the mods - we'll put them back later with our modifications appended */
  3405. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  3406. /* We need the resulting entry after the mods are applied to
  3407. * see if the entry is within the scope. */
  3408. resulting_e = slapi_entry_dup(e);
  3409. if (mods &&
  3410. (slapi_entry_apply_mods(resulting_e, mods) != LDAP_SUCCESS)) {
  3411. /* The mods don't apply cleanly, so we just let this op go
  3412. * to let the main server handle it. */
  3413. goto bail;
  3414. }
  3415. smods = slapi_mods_new();
  3416. slapi_mods_init_passin(smods, mods);
  3417. }
  3418. /* For a MOD, we need to check the resulting entry */
  3419. if (LDAP_CHANGETYPE_ADD == modtype) {
  3420. test_e = e;
  3421. } else {
  3422. test_e = resulting_e;
  3423. }
  3424. if (dna_dn_is_config(dn)) {
  3425. /* Validate config changes, but don't apply them.
  3426. * This allows us to reject invalid config changes
  3427. * here at the pre-op stage. Applying the config
  3428. * needs to be done at the post-op stage. */
  3429. if (dna_parse_config_entry(pb, test_e, 0) != DNA_SUCCESS) {
  3430. /* Refuse the operation if config parsing failed. */
  3431. ret = LDAP_UNWILLING_TO_PERFORM;
  3432. if (LDAP_CHANGETYPE_ADD == modtype) {
  3433. errstr = slapi_ch_smprintf("Not a valid DNA configuration entry.");
  3434. } else {
  3435. errstr = slapi_ch_smprintf("Changes result in an invalid "
  3436. "DNA configuration.");
  3437. }
  3438. }
  3439. } else {
  3440. if (LDAP_CHANGETYPE_ADD == modtype) {
  3441. ret = _dna_pre_op_add(pb, test_e, &errstr);
  3442. } else {
  3443. if((ret = _dna_pre_op_modify(pb, test_e, smods, &errstr))){
  3444. slapi_mods_free(&smods);
  3445. }
  3446. }
  3447. if (ret) {
  3448. goto bail;
  3449. }
  3450. }
  3451. /* We're done. */
  3452. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3453. /* Put the updated mods back into place. */
  3454. mods = slapi_mods_get_ldapmods_passout(smods);
  3455. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  3456. slapi_mods_free(&smods);
  3457. }
  3458. bail:
  3459. if (resulting_e)
  3460. slapi_entry_free(resulting_e);
  3461. slapi_mods_free(&smods);
  3462. if (ret) {
  3463. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3464. "dna_pre_op: operation failure [%d]\n", ret);
  3465. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  3466. slapi_ch_free((void **)&errstr);
  3467. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  3468. ret = DNA_FAILURE;
  3469. }
  3470. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3471. "<-- dna_pre_op\n");
  3472. return ret;
  3473. }
  3474. static int dna_add_pre_op(Slapi_PBlock * pb)
  3475. {
  3476. return dna_pre_op(pb, LDAP_CHANGETYPE_ADD);
  3477. }
  3478. static int dna_mod_pre_op(Slapi_PBlock * pb)
  3479. {
  3480. return dna_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  3481. }
  3482. static int dna_be_txn_add_pre_op(Slapi_PBlock *pb)
  3483. {
  3484. return dna_be_txn_pre_op(pb, LDAP_CHANGETYPE_ADD);
  3485. }
  3486. static int dna_be_txn_mod_pre_op(Slapi_PBlock *pb)
  3487. {
  3488. return dna_be_txn_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  3489. }
  3490. /*
  3491. * dna_be_txn_pre_op()
  3492. *
  3493. * In the preop if we found that we need to update a DNA attribute,
  3494. * We set the value to "-2" or DNA_NEEDS_UPDATE, because we don't want
  3495. * to do the value allocation in the preop as the operation could fail -
  3496. * resulting in lost values from the range. So we need to to ensure
  3497. * that the value will not be lost by performing the value allocation
  3498. * in the backend txn preop.
  3499. *
  3500. * Although the modifications have already been applied in backend,
  3501. * we still need to add the modification of the real value to the
  3502. * existing Slapi_Mods, so that the value gets indexed correctly.
  3503. *
  3504. * Also, since the modifications have already been applied to the entry
  3505. * in the backend, we need to manually update the entry with the new value.
  3506. */
  3507. static int dna_be_txn_pre_op(Slapi_PBlock *pb, int modtype)
  3508. {
  3509. struct configEntry *config_entry = NULL;
  3510. struct slapi_entry *e = NULL;
  3511. Slapi_Mods *smods = NULL;
  3512. Slapi_Mod *smod = NULL;
  3513. Slapi_Mod *next_mod = NULL;
  3514. Slapi_Attr *attr = NULL;
  3515. LDAPMod **mods = NULL;
  3516. struct berval *bv = NULL;
  3517. PRCList *list = NULL;
  3518. char *value = NULL;
  3519. char **types_to_generate = NULL;
  3520. char **generated_types = NULL;
  3521. char *new_value = NULL;
  3522. char *errstr = NULL;
  3523. char *dn = NULL;
  3524. char *type = NULL;
  3525. int numvals, e_numvals = 0;
  3526. int i, len, ret = DNA_SUCCESS;
  3527. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3528. "--> dna_be_txn_pre_op\n");
  3529. if (!slapi_plugin_running(pb)) {
  3530. goto bail;
  3531. }
  3532. if (0 == (dn = dna_get_dn(pb))) {
  3533. goto bail;
  3534. }
  3535. if (dna_dn_is_config(dn)) {
  3536. goto bail;
  3537. }
  3538. if (dna_isrepl(pb)) {
  3539. /* if repl, the dna values should be already in the entry. */
  3540. goto bail;
  3541. }
  3542. if (LDAP_CHANGETYPE_ADD == modtype) {
  3543. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  3544. } else {
  3545. slapi_pblock_get(pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  3546. }
  3547. if (e == NULL) {
  3548. goto bail;
  3549. } else if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3550. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  3551. smods = slapi_mods_new();
  3552. slapi_mods_init_passin(smods, mods);
  3553. }
  3554. dna_read_lock();
  3555. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3556. list = PR_LIST_HEAD(dna_global_config);
  3557. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  3558. config_entry = (struct configEntry *) list;
  3559. /* Did we already service all of these configured types? */
  3560. if (dna_list_contains_types(generated_types, config_entry->types)) {
  3561. goto next;
  3562. }
  3563. /* is the entry in scope? */
  3564. if (config_entry->scope) {
  3565. if (!slapi_dn_issuffix(dn, config_entry->scope))
  3566. goto next;
  3567. }
  3568. /* is this entry in an excluded scope? */
  3569. for (i = 0; config_entry->excludescope && config_entry->excludescope[i]; i++) {
  3570. if (slapi_dn_issuffix(dn, slapi_sdn_get_dn(config_entry->excludescope[i]))) {
  3571. goto next;
  3572. }
  3573. }
  3574. /* does the entry match the filter? */
  3575. if (config_entry->slapi_filter) {
  3576. if(LDAP_SUCCESS != slapi_vattr_filter_test(pb,e,config_entry->slapi_filter, 0))
  3577. goto next;
  3578. }
  3579. if (LDAP_CHANGETYPE_ADD == modtype) {
  3580. if (dna_is_multitype_range(config_entry)) {
  3581. /* For a multi-type range, we only generate a value
  3582. * for types where the magic value is set. We do not
  3583. * generate a value for missing types. */
  3584. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  3585. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  3586. if (value && !slapi_UTF8CASECMP(value, DNA_NEEDS_UPDATE)) {
  3587. slapi_ch_array_add(&types_to_generate,
  3588. slapi_ch_strdup(config_entry->types[i]));
  3589. /* Need to remove DNA_NEEDS_UPDATE */
  3590. slapi_entry_attr_delete(e, config_entry->types[i]);
  3591. }
  3592. slapi_ch_free_string(&value);
  3593. }
  3594. } else {
  3595. /* For a single type range, we generate the value if
  3596. * the magic value is set or if the type is missing. */
  3597. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  3598. if (0 == value || (value && !slapi_UTF8CASECMP(value, DNA_NEEDS_UPDATE)) ) {
  3599. slapi_ch_array_add(&types_to_generate,
  3600. slapi_ch_strdup(config_entry->types[0]));
  3601. /* Need to remove DNA_NEEDS_UPDATE */
  3602. slapi_entry_attr_delete(e, config_entry->types[0]);
  3603. }
  3604. slapi_ch_free_string(&value);
  3605. }
  3606. } else {
  3607. /* check mods for DNA_NEEDS_UPDATE*/
  3608. next_mod = slapi_mod_new();
  3609. smod = slapi_mods_get_first_smod(smods, next_mod);
  3610. while (smod) {
  3611. type = (char *)slapi_mod_get_type(smod);
  3612. /* See if the type matches any configured type. */
  3613. if (dna_list_contains_type(config_entry->types, type)) {
  3614. /* If all values are being deleted, we need to
  3615. * generate a new value. */
  3616. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  3617. !dna_is_multitype_range(config_entry)) {
  3618. numvals = slapi_mod_get_num_values(smod);
  3619. if (numvals == 0) {
  3620. slapi_ch_array_add(&types_to_generate,slapi_ch_strdup(type));
  3621. } else {
  3622. slapi_entry_attr_find(e, type, &attr);
  3623. if (attr) {
  3624. slapi_attr_get_numvalues(attr, &e_numvals);
  3625. if (numvals >= e_numvals) {
  3626. slapi_ch_array_add(&types_to_generate,
  3627. slapi_ch_strdup(type));
  3628. }
  3629. }
  3630. }
  3631. } else {
  3632. /* This is either adding or replacing a value */
  3633. bv = slapi_mod_get_first_value(smod);
  3634. if (dna_list_contains_type(types_to_generate, type)) {
  3635. dna_list_remove_type(types_to_generate, type);
  3636. }
  3637. /* If we have a value, see if it's the magic value. */
  3638. if (bv) {
  3639. if (!slapi_UTF8CASECMP(bv->bv_val,
  3640. DNA_NEEDS_UPDATE)) {
  3641. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3642. }
  3643. } else if (!dna_is_multitype_range(config_entry)) {
  3644. /* This is a replace with no new values, so we need
  3645. * to generate a new value if this is not a multi-type range. */
  3646. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(type));
  3647. }
  3648. }
  3649. }
  3650. slapi_mod_done(next_mod);
  3651. smod = slapi_mods_get_next_smod(smods, next_mod);
  3652. }
  3653. slapi_mod_free(&next_mod);
  3654. }
  3655. /* We need to perform one last check for modify operations. If an
  3656. * entry within the scope has not triggered generation yet, we need
  3657. * to see if a value exists for the managed type in the resulting
  3658. * entry. This will catch a modify operation that brings an entry
  3659. * into scope for a managed range, but doesn't supply a value for
  3660. * the managed type. We don't do this for multi-type ranges. */
  3661. if ((LDAP_CHANGETYPE_MODIFY == modtype) && (!types_to_generate ||
  3662. (types_to_generate && !types_to_generate[0])) && !dna_is_multitype_range(config_entry)) {
  3663. if (slapi_entry_attr_find(e, config_entry->types[0], &attr) != 0) {
  3664. slapi_ch_array_add(&types_to_generate, slapi_ch_strdup(config_entry->types[0]));
  3665. }
  3666. }
  3667. if (types_to_generate && types_to_generate[0]) {
  3668. /* create the value to add */
  3669. ret = dna_get_next_value(config_entry, &value);
  3670. if (DNA_SUCCESS != ret) {
  3671. errstr = slapi_ch_smprintf("Allocation of a new value for range"
  3672. " %s failed! Unable to proceed.",
  3673. config_entry->dn);
  3674. slapi_ch_array_free(types_to_generate);
  3675. break;
  3676. }
  3677. len = strlen(value) + 1;
  3678. if (config_entry->prefix) {
  3679. len += strlen(config_entry->prefix);
  3680. }
  3681. new_value = slapi_ch_malloc(len);
  3682. if (config_entry->prefix) {
  3683. strcpy(new_value, config_entry->prefix);
  3684. strcat(new_value, value);
  3685. } else
  3686. strcpy(new_value, value);
  3687. /* do the mod */
  3688. if (LDAP_CHANGETYPE_ADD == modtype) {
  3689. /* add - add to entry */
  3690. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3691. slapi_entry_attr_set_charptr(e, types_to_generate[i], new_value);
  3692. }
  3693. } else {
  3694. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  3695. slapi_mods_add_string(smods, LDAP_MOD_REPLACE, types_to_generate[i], new_value);
  3696. /* we need to directly update the entry in be_txn_preop */
  3697. slapi_entry_attr_set_charptr(e, types_to_generate[i], new_value);
  3698. }
  3699. }
  3700. /*
  3701. * Make sure we don't generate for this
  3702. * type again by keeping a list of types
  3703. * we have generated for already.
  3704. */
  3705. if (LDAP_SUCCESS == ret) {
  3706. if (generated_types == NULL) {
  3707. /* If we don't have a list of generated types yet,
  3708. * we can just use the types_to_generate list so
  3709. * we don't have to allocate anything. */
  3710. generated_types = types_to_generate;
  3711. types_to_generate = NULL;
  3712. } else {
  3713. /* Just reuse the elements out of types_to_generate for the
  3714. * generated types list to avoid allocating them again. */
  3715. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  3716. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  3717. types_to_generate[i] = NULL;
  3718. }
  3719. }
  3720. }
  3721. /* free up */
  3722. slapi_ch_free_string(&value);
  3723. slapi_ch_free_string(&new_value);
  3724. slapi_ch_array_free(types_to_generate);
  3725. types_to_generate = NULL;
  3726. } else if (types_to_generate) {
  3727. slapi_ch_free((void **)&types_to_generate);
  3728. }
  3729. next:
  3730. list = PR_NEXT_LINK(list);
  3731. }
  3732. }
  3733. dna_unlock();
  3734. bail:
  3735. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  3736. /* Put the updated mods back into place. */
  3737. if (smods) { /* smods == NULL if we bailed before initializing it */
  3738. mods = slapi_mods_get_ldapmods_passout(smods);
  3739. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  3740. slapi_mods_free(&smods);
  3741. }
  3742. }
  3743. slapi_ch_array_free(generated_types);
  3744. if (ret) {
  3745. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3746. "dna_be_txn_pre_op: operation failure [%d]\n", ret);
  3747. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  3748. slapi_ch_free((void **)&errstr);
  3749. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  3750. ret = DNA_FAILURE;
  3751. }
  3752. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3753. "<-- dna_be_txn_pre_op\n");
  3754. return ret;
  3755. }
  3756. static int dna_config_check_post_op(Slapi_PBlock * pb)
  3757. {
  3758. char *dn;
  3759. if(!slapi_plugin_running(pb)){
  3760. return DNA_SUCCESS;
  3761. }
  3762. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3763. "--> dna_config_check_post_op\n");
  3764. if (!slapi_op_internal(pb)) { /* If internal, no need to check. */
  3765. if ((dn = dna_get_dn(pb))) {
  3766. if (dna_dn_is_config(dn)) {
  3767. dna_load_plugin_config(pb, 0);
  3768. }
  3769. if(dna_dn_is_shared_config(pb, dn)){
  3770. dna_load_shared_servers();
  3771. }
  3772. }
  3773. }
  3774. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3775. "<-- dna_config_check_post_op\n");
  3776. return DNA_SUCCESS;
  3777. }
  3778. /****************************************************
  3779. * Pre Extended Operation, Backend selection
  3780. ***************************************************/
  3781. static int dna_extend_exop_backend(Slapi_PBlock *pb, Slapi_Backend **target)
  3782. {
  3783. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3784. "--> dna_parse_exop_backend\n");
  3785. Slapi_DN *shared_sdn = NULL;
  3786. char *shared_dn = NULL;
  3787. int res = -1;
  3788. /* Parse the oid and what exop wants us to do */
  3789. res = dna_parse_exop_ber(pb, &shared_dn);
  3790. if (res != LDAP_SUCCESS) {
  3791. return res;
  3792. }
  3793. if (shared_dn) {
  3794. shared_sdn = slapi_sdn_new_dn_byref(shared_dn);
  3795. *target = slapi_be_select(shared_sdn);
  3796. slapi_sdn_free(&shared_sdn);
  3797. }
  3798. res = LDAP_SUCCESS;
  3799. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3800. "<-- dna_parse_exop_backend %d\n", res);
  3801. return res;
  3802. }
  3803. /****************************************************
  3804. * Range Extension Extended Operation
  3805. ***************************************************/
  3806. static int dna_extend_exop(Slapi_PBlock *pb)
  3807. {
  3808. int ret = SLAPI_PLUGIN_EXTENDED_NOT_HANDLED;
  3809. char *shared_dn = NULL;
  3810. char *bind_dn = NULL;
  3811. PRUint64 lower = 0;
  3812. PRUint64 upper = 0;
  3813. if(!slapi_plugin_running(pb)){
  3814. return ret;
  3815. }
  3816. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3817. "--> dna_extend_exop\n");
  3818. if(dna_parse_exop_ber(pb, &shared_dn) != LDAP_SUCCESS) {
  3819. return ret;
  3820. }
  3821. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3822. "dna_extend_exop: received range extension "
  3823. "request for range [%s]\n", shared_dn);
  3824. /* Only allow range requests from the replication bind DN user */
  3825. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
  3826. if (!dna_is_replica_bind_dn(shared_dn, bind_dn)) {
  3827. ret = LDAP_INSUFFICIENT_ACCESS;
  3828. goto free_and_return;
  3829. }
  3830. /* See if we have the req. range configured.
  3831. * If so, we need to see if we have range to provide. */
  3832. ret = dna_release_range(shared_dn, &lower, &upper);
  3833. if (ret == LDAP_SUCCESS) {
  3834. /* We have range to give away, so construct
  3835. * and send the response. */
  3836. BerElement *respber = NULL;
  3837. struct berval *respdata = NULL;
  3838. struct berval range_low = {0, NULL};
  3839. struct berval range_high = {0, NULL};
  3840. char lowstr[16];
  3841. char highstr[16];
  3842. /* Create the exop response */
  3843. PR_snprintf(lowstr, sizeof(lowstr), "%" NSPRIu64, lower);
  3844. PR_snprintf(highstr, sizeof(highstr), "%" NSPRIu64, upper);
  3845. range_low.bv_val = lowstr;
  3846. range_low.bv_len = strlen(range_low.bv_val);
  3847. range_high.bv_val = highstr;
  3848. range_high.bv_len = strlen(range_high.bv_val);
  3849. if ((respber = ber_alloc()) == NULL) {
  3850. ret = LDAP_NO_MEMORY;
  3851. goto free_and_return;
  3852. }
  3853. if (LBER_ERROR == (ber_printf(respber, "{oo}",
  3854. range_low.bv_val, range_low.bv_len,
  3855. range_high.bv_val, range_high.bv_len))) {
  3856. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  3857. "dna_extend_exop: Unable to encode exop response.\n");
  3858. ber_free(respber, 1);
  3859. ret = LDAP_ENCODING_ERROR;
  3860. goto free_and_return;
  3861. }
  3862. ber_flatten(respber, &respdata);
  3863. ber_free(respber, 1);
  3864. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, DNA_EXTEND_EXOP_RESPONSE_OID);
  3865. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, respdata);
  3866. /* send the response ourselves */
  3867. slapi_send_ldap_result( pb, ret, NULL, NULL, 0, NULL );
  3868. ret = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  3869. ber_bvfree(respdata);
  3870. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3871. "dna_extend_exop: Released range %" NSPRIu64 "-%" NSPRIu64 ".\n",
  3872. lower, upper);
  3873. }
  3874. free_and_return:
  3875. slapi_ch_free_string(&shared_dn);
  3876. slapi_ch_free_string(&bind_dn);
  3877. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3878. "<-- dna_extend_exop\n");
  3879. return ret;
  3880. }
  3881. /*
  3882. * dna_release_range()
  3883. *
  3884. * Checks if we have any values that we can release
  3885. * for the range specified by range_dn.
  3886. */
  3887. static int
  3888. dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper)
  3889. {
  3890. int ret = 0;
  3891. int match = 0;
  3892. PRCList *list = NULL;
  3893. Slapi_DN *cfg_base_sdn = NULL;
  3894. Slapi_DN *range_sdn = NULL;
  3895. struct configEntry *config_entry = NULL;
  3896. int set_extend_flag = 0;
  3897. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  3898. "--> dna_release_range\n");
  3899. if (range_dn) {
  3900. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  3901. dna_read_lock();
  3902. /* Go through the config entries to see if we
  3903. * have a shared range configured that matches
  3904. * the range from the exop request. */
  3905. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  3906. list = PR_LIST_HEAD(dna_global_config);
  3907. while ((list != dna_global_config) && match != 1) {
  3908. config_entry = (struct configEntry *)list;
  3909. cfg_base_sdn = slapi_sdn_new_normdn_byref(config_entry->shared_cfg_base);
  3910. if (slapi_sdn_compare(cfg_base_sdn, range_sdn) == 0) {
  3911. /* We found a match. Set match flag to
  3912. * break out of the loop. */
  3913. match = 1;
  3914. } else {
  3915. config_entry = NULL;
  3916. list = PR_NEXT_LINK(list);
  3917. }
  3918. slapi_sdn_free(&cfg_base_sdn);
  3919. }
  3920. }
  3921. /* config_entry will point to our match if we found one */
  3922. if (config_entry) {
  3923. int release = 0;
  3924. Slapi_PBlock *pb = NULL;
  3925. LDAPMod mod_replace;
  3926. LDAPMod *mods[2];
  3927. char *replace_val[2];
  3928. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  3929. char max_value[17];
  3930. /* Need to bail if we're performing a range request
  3931. * for this range. This is to prevent the case where two
  3932. * servers are asking each other for more range for the
  3933. * same managed range. This would result in a network
  3934. * deadlock until the idletimeout kills one of the
  3935. * connections. */
  3936. slapi_lock_mutex(config_entry->extend_lock);
  3937. if (config_entry->extend_in_progress) {
  3938. /* We're already processing a range extention, so bail. */
  3939. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  3940. "dna_release_range: Already processing a "
  3941. "range extension request. Skipping request.\n");
  3942. slapi_unlock_mutex(config_entry->extend_lock);
  3943. ret = LDAP_UNWILLING_TO_PERFORM;
  3944. goto bail;
  3945. } else {
  3946. /* Set a flag indicating that we're attempting to extend this range */
  3947. config_entry->extend_in_progress = 1;
  3948. set_extend_flag = 1;
  3949. slapi_unlock_mutex(config_entry->extend_lock);
  3950. }
  3951. /* Obtain the lock for this range */
  3952. slapi_lock_mutex(config_entry->lock);
  3953. /* Refuse if we're at or below our threshold */
  3954. if (config_entry->remaining <= config_entry->threshold) {
  3955. ret = LDAP_UNWILLING_TO_PERFORM;
  3956. goto bail;
  3957. }
  3958. /* If we have a next range, we need to give up values from
  3959. * it instead of from the active range */
  3960. if (config_entry->next_range_lower != 0) {
  3961. /* Release up to half of our values from the next range. */
  3962. release = (((config_entry->next_range_upper - config_entry->next_range_lower + 1) /
  3963. 2) / config_entry->threshold) * config_entry->threshold;
  3964. if (release == 0) {
  3965. ret = LDAP_UNWILLING_TO_PERFORM;
  3966. goto bail;
  3967. }
  3968. *upper = config_entry->next_range_upper;
  3969. *lower = *upper - release + 1;
  3970. /* Try to set the new next range in the config */
  3971. ret = dna_update_next_range(config_entry, config_entry->next_range_lower,
  3972. *lower - 1);
  3973. } else {
  3974. /* We release up to half of our remaining values,
  3975. * but we'll only release a range that is a multiple
  3976. * of our threshold. */
  3977. release = ((config_entry->remaining / 2) /
  3978. config_entry->threshold) * config_entry->threshold;
  3979. if (release == 0) {
  3980. ret = LDAP_UNWILLING_TO_PERFORM;
  3981. goto bail;
  3982. }
  3983. /* We give away values from the upper end of our range. */
  3984. *upper = config_entry->maxval;
  3985. *lower = *upper - release + 1;
  3986. /* try to set the new maxval in the config entry */
  3987. PR_snprintf(max_value, sizeof(max_value),"%" NSPRIu64, (*lower - 1));
  3988. /* set up our replace modify operation */
  3989. replace_val[0] = max_value;
  3990. replace_val[1] = 0;
  3991. mod_replace.mod_op = LDAP_MOD_REPLACE;
  3992. mod_replace.mod_type = DNA_MAXVAL;
  3993. mod_replace.mod_values = replace_val;
  3994. mods[0] = &mod_replace;
  3995. mods[1] = 0;
  3996. pb = slapi_pblock_new();
  3997. if (NULL == pb) {
  3998. ret = LDAP_OPERATIONS_ERROR;
  3999. goto bail;
  4000. }
  4001. slapi_modify_internal_set_pb(pb, config_entry->dn,
  4002. mods, 0, 0, getPluginID(), 0);
  4003. slapi_modify_internal_pb(pb);
  4004. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  4005. slapi_pblock_destroy(pb);
  4006. pb = NULL;
  4007. if (ret == LDAP_SUCCESS) {
  4008. /* Adjust maxval in our cached config and shared config */
  4009. config_entry->maxval = *lower - 1;
  4010. /* This is within the dna_lock, so okay */
  4011. dna_notice_allocation(config_entry, config_entry->nextval, 0);
  4012. }
  4013. }
  4014. if (ret != LDAP_SUCCESS) {
  4015. /* Updating the config failed, so reset. We don't
  4016. * want to give the caller any range */
  4017. *lower = 0;
  4018. *upper = 0;
  4019. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  4020. "dna_release_range: Error updating "
  4021. "configuration entry [err=%d]\n", ret);
  4022. }
  4023. }
  4024. bail:
  4025. if (set_extend_flag) {
  4026. slapi_lock_mutex(config_entry->extend_lock);
  4027. config_entry->extend_in_progress = 0;
  4028. slapi_unlock_mutex(config_entry->extend_lock);
  4029. }
  4030. if (config_entry) {
  4031. slapi_unlock_mutex(config_entry->lock);
  4032. }
  4033. slapi_sdn_free(&range_sdn);
  4034. dna_unlock();
  4035. }
  4036. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  4037. "<-- dna_release_range\n");
  4038. return ret;
  4039. }
  4040. /****************************************************
  4041. End of
  4042. Functions that actually do things other
  4043. than config and startup
  4044. ****************************************************/
  4045. /**
  4046. * debug functions to print config
  4047. */
  4048. void dna_dump_config()
  4049. {
  4050. PRCList *list;
  4051. dna_read_lock();
  4052. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  4053. list = PR_LIST_HEAD(dna_global_config);
  4054. while (list != dna_global_config) {
  4055. dna_dump_config_entry((struct configEntry *) list);
  4056. list = PR_NEXT_LINK(list);
  4057. }
  4058. }
  4059. dna_unlock();
  4060. }
  4061. /*
  4062. * dna_isrepl()
  4063. *
  4064. * Returns 1 if the operation associated with pb
  4065. * is a replicated op. Returns 0 otherwise.
  4066. */
  4067. static int
  4068. dna_isrepl(Slapi_PBlock *pb)
  4069. {
  4070. int is_repl = 0;
  4071. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  4072. return is_repl;
  4073. }
  4074. void dna_dump_config_entry(struct configEntry * entry)
  4075. {
  4076. int i = 0;
  4077. for (i = 0; entry->types && entry->types[i]; i++) {
  4078. printf("<---- type -----------> %s\n", entry->types[i]);
  4079. }
  4080. printf("<---- filter ---------> %s\n", entry->filter);
  4081. printf("<---- prefix ---------> %s\n", entry->prefix);
  4082. printf("<---- scope ----------> %s\n", entry->scope);
  4083. for (i = 0; entry->excludescope && entry->excludescope[i]; i++) {
  4084. printf("<---- excluded scope -> %s\n", slapi_sdn_get_dn(entry->excludescope[i]));
  4085. }
  4086. printf("<---- next value -----> %" PRIu64 "\n", entry->nextval);
  4087. printf("<---- max value ------> %" PRIu64 "\n", entry->maxval);
  4088. printf("<---- interval -------> %" PRIu64 "\n", entry->interval);
  4089. printf("<---- generate flag --> %s\n", entry->generate);
  4090. printf("<---- shared cfg base > %s\n", entry->shared_cfg_base);
  4091. printf("<---- shared cfg DN --> %s\n", entry->shared_cfg_dn);
  4092. printf("<---- threshold ------> %" PRIu64 "", entry->threshold);
  4093. }