dna.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2007 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /**
  41. * Distributed Numeric Assignment plug-in
  42. */
  43. #include <stdio.h>
  44. #include <ctype.h>
  45. #include <string.h>
  46. #include <errno.h>
  47. #include "portable.h"
  48. #include "nspr.h"
  49. #include "slapi-private.h"
  50. #include "prclist.h"
  51. /* Required to get portable printf/scanf format macros */
  52. #ifdef HAVE_INTTYPES_H
  53. #include <inttypes.h>
  54. #else
  55. #error Need to define portable format macros such as PRIu64
  56. #endif /* HAVE_INTTYPES_H */
  57. /* get file mode flags for unix */
  58. #ifndef _WIN32
  59. #include <sys/stat.h>
  60. #endif
  61. #define DNA_PLUGIN_SUBSYSTEM "dna-plugin"
  62. #define DNA_PLUGIN_VERSION 0x00020000
  63. #define DNA_DN "cn=Distributed Numeric Assignment Plugin,cn=plugins,cn=config" /* temporary */
  64. #define DNA_SUCCESS 0
  65. #define DNA_FAILURE -1
  66. /* Default range request timeout */
  67. #define DNA_DEFAULT_TIMEOUT 10
  68. /**
  69. * DNA config types
  70. */
  71. #define DNA_TYPE "dnaType"
  72. #define DNA_PREFIX "dnaPrefix"
  73. #define DNA_NEXTVAL "dnaNextValue"
  74. #define DNA_INTERVAL "dnaInterval"
  75. #define DNA_GENERATE "dnaMagicRegen"
  76. #define DNA_FILTER "dnaFilter"
  77. #define DNA_SCOPE "dnaScope"
  78. /* since v2 */
  79. #define DNA_MAXVAL "dnaMaxValue"
  80. #define DNA_SHARED_CFG_DN "dnaSharedCfgDN"
  81. /* Shared Config */
  82. #define DNA_REMAINING "dnaRemainingValues"
  83. #define DNA_THRESHOLD "dnaThreshold"
  84. #define DNA_HOSTNAME "dnaHostname"
  85. #define DNA_PORTNUM "dnaPortNum"
  86. #define DNA_SECURE_PORTNUM "dnaSecurePortNum"
  87. /* For transferred ranges */
  88. #define DNA_NEXT_RANGE "dnaNextRange"
  89. #define DNA_RANGE_REQUEST_TIMEOUT "dnaRangeRequestTimeout"
  90. /* Replication types */
  91. #define DNA_REPL_BIND_DN "nsds5ReplicaBindDN"
  92. #define DNA_REPL_CREDS "nsds5ReplicaCredentials"
  93. #define DNA_REPL_BIND_METHOD "nsds5ReplicaBindMethod"
  94. #define DNA_REPL_TRANSPORT "nsds5ReplicaTransportInfo"
  95. #define DNA_REPL_PORT "nsds5ReplicaPort"
  96. #define DNA_FEATURE_DESC "Distributed Numeric Assignment"
  97. #define DNA_EXOP_FEATURE_DESC "DNA Range Extension Request"
  98. #define DNA_PLUGIN_DESC "Distributed Numeric Assignment plugin"
  99. #define DNA_INT_PREOP_DESC "Distributed Numeric Assignment internal preop plugin"
  100. #define DNA_POSTOP_DESC "Distributed Numeric Assignment postop plugin"
  101. #define DNA_EXOP_DESC "Distributed Numeric Assignment range extension extop plugin"
  102. #define INTEGER_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.27"
  103. #define DNA_EXTEND_EXOP_REQUEST_OID "2.16.840.1.113730.3.5.10"
  104. #define DNA_EXTEND_EXOP_RESPONSE_OID "2.16.840.1.113730.3.5.11"
  105. static Slapi_PluginDesc pdesc = { DNA_FEATURE_DESC,
  106. VENDOR,
  107. DS_PACKAGE_VERSION,
  108. DNA_PLUGIN_DESC };
  109. static Slapi_PluginDesc exop_pdesc = { DNA_EXOP_FEATURE_DESC,
  110. VENDOR,
  111. DS_PACKAGE_VERSION,
  112. DNA_EXOP_DESC };
  113. /**
  114. * linked list of config entries
  115. */
  116. struct configEntry {
  117. PRCList list;
  118. char *dn;
  119. char **types;
  120. char *prefix;
  121. char *filter;
  122. Slapi_Filter *slapi_filter;
  123. char *generate;
  124. char *scope;
  125. PRUint64 interval;
  126. PRUint64 threshold;
  127. char *shared_cfg_base;
  128. char *shared_cfg_dn;
  129. PRUint64 timeout;
  130. /* This lock protects the 5 members below. All
  131. * of the above members are safe to read as long
  132. * as you call dna_read_lock() first. */
  133. Slapi_Mutex *lock;
  134. PRUint64 nextval;
  135. PRUint64 maxval;
  136. PRUint64 remaining;
  137. PRUint64 next_range_lower;
  138. PRUint64 next_range_upper;
  139. /* This lock protects the extend_in_progress
  140. * member. This is used to prevent us from
  141. * processing a range extention request and
  142. * trying to extend out own range at the same
  143. * time. */
  144. Slapi_Mutex *extend_lock;
  145. int extend_in_progress;
  146. };
  147. static PRCList *dna_global_config = NULL;
  148. static PRRWLock *g_dna_cache_lock;
  149. static void *_PluginID = NULL;
  150. static char *_PluginDN = NULL;
  151. static int g_plugin_started = 0;
  152. static char *hostname = NULL;
  153. static char *portnum = NULL;
  154. static char *secureportnum = NULL;
  155. /**
  156. * server struct for shared ranges
  157. */
  158. struct dnaServer {
  159. PRCList list;
  160. char *host;
  161. unsigned int port;
  162. unsigned int secureport;
  163. PRUint64 remaining;
  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_internal_preop_init(Slapi_PBlock *pb);
  178. static int dna_postop_init(Slapi_PBlock * pb);
  179. static int dna_exop_init(Slapi_PBlock * pb);
  180. /**
  181. *
  182. * Local operation functions
  183. *
  184. */
  185. static int dna_load_plugin_config();
  186. static int dna_parse_config_entry(Slapi_Entry * e, int apply);
  187. static void dna_delete_config();
  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. static void dna_notice_allocation(struct configEntry *config_entry,
  203. PRUint64 new, PRUint64 last, int fix);
  204. static int dna_update_shared_config(struct configEntry * config_entry);
  205. static void dna_update_config_event(time_t event_time, void *arg);
  206. static int dna_get_shared_servers(struct configEntry *config_entry, PRCList **servers);
  207. static void dna_free_shared_server(struct dnaServer **server);
  208. static void dna_delete_shared_servers(PRCList **servers);
  209. static int dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper);
  210. static int dna_request_range(struct configEntry *config_entry,
  211. struct dnaServer *server,
  212. PRUint64 *lower, PRUint64 *upper);
  213. static struct berval *dna_create_range_request(char *range_dn);
  214. static int dna_update_next_range(struct configEntry *config_entry,
  215. PRUint64 lower, PRUint64 upper);
  216. static int dna_activate_next_range(struct configEntry *config_entry);
  217. static int dna_is_replica_bind_dn(char *range_dn, char *bind_dn);
  218. static int dna_get_replica_bind_creds(char *range_dn, struct dnaServer *server,
  219. char **bind_dn, char **bind_passwd,
  220. char **bind_method, int *is_ssl, int *port);
  221. static int dna_list_contains_type(char **list, char *type);
  222. static int dna_list_contains_types(char **list, char **types);
  223. static void dna_list_remove_type(char **list, char *type);
  224. static int dna_is_multitype_range(struct configEntry *config_entry);
  225. static void dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter);
  226. /**
  227. *
  228. * the ops (where the real work is done)
  229. *
  230. */
  231. static int dna_config_check_post_op(Slapi_PBlock * pb);
  232. static int dna_pre_op(Slapi_PBlock * pb, int modtype);
  233. static int dna_mod_pre_op(Slapi_PBlock * pb);
  234. static int dna_add_pre_op(Slapi_PBlock * pb);
  235. static int dna_extend_exop(Slapi_PBlock *pb);
  236. /**
  237. * debug functions - global, for the debugger
  238. */
  239. void dna_dump_config();
  240. void dna_dump_config_entry(struct configEntry *);
  241. /**
  242. * set the debug level
  243. */
  244. #ifdef _WIN32
  245. int *module_ldap_debug = 0;
  246. void plugin_init_debug_level(int *level_ptr)
  247. {
  248. module_ldap_debug = level_ptr;
  249. }
  250. #endif
  251. /**
  252. *
  253. * Deal with cache locking
  254. *
  255. */
  256. void dna_read_lock()
  257. {
  258. PR_RWLock_Rlock(g_dna_cache_lock);
  259. }
  260. void dna_write_lock()
  261. {
  262. PR_RWLock_Wlock(g_dna_cache_lock);
  263. }
  264. void dna_unlock()
  265. {
  266. PR_RWLock_Unlock(g_dna_cache_lock);
  267. }
  268. /**
  269. *
  270. * Get the dna plug-in version
  271. *
  272. */
  273. int dna_version()
  274. {
  275. return DNA_PLUGIN_VERSION;
  276. }
  277. /**
  278. * Plugin identity mgmt
  279. */
  280. void setPluginID(void *pluginID)
  281. {
  282. _PluginID = pluginID;
  283. }
  284. void *getPluginID()
  285. {
  286. return _PluginID;
  287. }
  288. void setPluginDN(char *pluginDN)
  289. {
  290. _PluginDN = pluginDN;
  291. }
  292. char *getPluginDN()
  293. {
  294. return _PluginDN;
  295. }
  296. /*
  297. dna_init
  298. -------------
  299. adds our callbacks to the list
  300. */
  301. int
  302. dna_init(Slapi_PBlock *pb)
  303. {
  304. int status = DNA_SUCCESS;
  305. char *plugin_identity = NULL;
  306. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  307. "--> dna_init\n");
  308. /**
  309. * Store the plugin identity for later use.
  310. * Used for internal operations
  311. */
  312. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  313. PR_ASSERT(plugin_identity);
  314. setPluginID(plugin_identity);
  315. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  316. SLAPI_PLUGIN_VERSION_01) != 0 ||
  317. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  318. (void *) dna_start) != 0 ||
  319. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  320. (void *) dna_close) != 0 ||
  321. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  322. (void *) &pdesc) != 0 ||
  323. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  324. (void *) dna_mod_pre_op) != 0 ||
  325. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  326. (void *) dna_add_pre_op) != 0 ||
  327. /* internal preoperation */
  328. slapi_register_plugin("internalpreoperation", /* op type */
  329. 1, /* Enabled */
  330. "dna_init", /* this function desc */
  331. dna_internal_preop_init, /* init func */
  332. DNA_INT_PREOP_DESC, /* plugin desc */
  333. NULL, /* ? */
  334. plugin_identity /* access control */
  335. ) ||
  336. /* the config change checking post op */
  337. slapi_register_plugin("postoperation", /* op type */
  338. 1, /* Enabled */
  339. "dna_init", /* this function desc */
  340. dna_postop_init, /* init func for post op */
  341. DNA_POSTOP_DESC, /* plugin desc */
  342. NULL, /* ? */
  343. plugin_identity /* access control */
  344. ) ||
  345. /* the range extension extended operation */
  346. slapi_register_plugin("extendedop", /* op type */
  347. 1, /* Enabled */
  348. "dna_init", /* this function desc */
  349. dna_exop_init, /* init func for exop */
  350. DNA_EXOP_DESC, /* plugin desc */
  351. NULL, /* ? */
  352. plugin_identity /* access control */
  353. )
  354. ) {
  355. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  356. "dna_init: failed to register plugin\n");
  357. status = DNA_FAILURE;
  358. }
  359. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  360. "<-- dna_init\n");
  361. return status;
  362. }
  363. static int
  364. dna_internal_preop_init(Slapi_PBlock *pb)
  365. {
  366. int status = DNA_SUCCESS;
  367. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  368. SLAPI_PLUGIN_VERSION_01) != 0 ||
  369. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  370. (void *) &pdesc) != 0 ||
  371. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
  372. (void *) dna_mod_pre_op) != 0 ||
  373. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
  374. (void *) dna_add_pre_op) != 0) {
  375. status = DNA_FAILURE;
  376. }
  377. return status;
  378. }
  379. static int
  380. dna_postop_init(Slapi_PBlock *pb)
  381. {
  382. int status = DNA_SUCCESS;
  383. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  384. SLAPI_PLUGIN_VERSION_01) != 0 ||
  385. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  386. (void *) &pdesc) != 0 ||
  387. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,
  388. (void *) dna_config_check_post_op) != 0 ||
  389. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN,
  390. (void *) dna_config_check_post_op) != 0 ||
  391. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN,
  392. (void *) dna_config_check_post_op) != 0 ||
  393. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN,
  394. (void *) dna_config_check_post_op) != 0) {
  395. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  396. "dna_postop_init: failed to register plugin\n");
  397. status = DNA_FAILURE;
  398. }
  399. return status;
  400. }
  401. static int
  402. dna_exop_init(Slapi_PBlock * pb)
  403. {
  404. int status = DNA_SUCCESS;
  405. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  406. SLAPI_PLUGIN_VERSION_01) != 0 ||
  407. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  408. (void *) &exop_pdesc) != 0 ||
  409. slapi_pblock_set(pb, SLAPI_PLUGIN_EXT_OP_OIDLIST,
  410. (void *) dna_extend_exop_oid_list) != 0 ||
  411. slapi_pblock_set(pb, SLAPI_PLUGIN_EXT_OP_FN,
  412. (void *) dna_extend_exop) != 0) {
  413. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  414. "dna_exop_init: failed to register plugin\n");
  415. status = DNA_FAILURE;
  416. }
  417. return status;
  418. }
  419. /*
  420. dna_start
  421. --------------
  422. Kicks off the config cache.
  423. It is called after dna_init.
  424. */
  425. static int
  426. dna_start(Slapi_PBlock * pb)
  427. {
  428. char *plugindn = NULL;
  429. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  430. "--> dna_start\n");
  431. /* Check if we're already started */
  432. if (g_plugin_started) {
  433. goto done;
  434. }
  435. g_dna_cache_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "dna");
  436. if (!g_dna_cache_lock) {
  437. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  438. "dna_start: lock creation failed\n");
  439. return DNA_FAILURE;
  440. }
  441. /**
  442. * Get the plug-in target dn from the system
  443. * and store it for future use. This should avoid
  444. * hardcoding of DN's in the code.
  445. */
  446. slapi_pblock_get(pb, SLAPI_TARGET_DN, &plugindn);
  447. if (NULL == plugindn || 0 == strlen(plugindn)) {
  448. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  449. "dna_start: had to use hard coded config dn\n");
  450. plugindn = DNA_DN;
  451. } else {
  452. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  453. "dna_start: config at %s\n", plugindn);
  454. }
  455. setPluginDN(plugindn);
  456. /* We need the host and port number of this server
  457. * in case shared config is enabled for any of the
  458. * ranges we are managing. */
  459. if (dna_load_host_port() != DNA_SUCCESS) {
  460. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  461. "dna_start: unable to load host and port information\n");
  462. }
  463. /*
  464. * Load the config for our plug-in
  465. */
  466. dna_global_config = (PRCList *)
  467. slapi_ch_calloc(1, sizeof(struct configEntry));
  468. PR_INIT_CLIST(dna_global_config);
  469. if (dna_load_plugin_config() != DNA_SUCCESS) {
  470. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  471. "dna_start: unable to load plug-in configuration\n");
  472. return DNA_FAILURE;
  473. }
  474. g_plugin_started = 1;
  475. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  476. "dna: ready for service\n");
  477. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  478. "<-- dna_start\n");
  479. done:
  480. return DNA_SUCCESS;
  481. }
  482. /*
  483. dna_close
  484. --------------
  485. closes down the cache
  486. */
  487. static int
  488. dna_close(Slapi_PBlock * pb)
  489. {
  490. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  491. "--> dna_close\n");
  492. dna_delete_config();
  493. slapi_ch_free((void **)&dna_global_config);
  494. slapi_ch_free_string(&hostname);
  495. slapi_ch_free_string(&portnum);
  496. slapi_ch_free_string(&secureportnum);
  497. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  498. "<-- dna_close\n");
  499. return DNA_SUCCESS;
  500. }
  501. /*
  502. * config looks like this
  503. * - cn=myplugin
  504. * --- cn=posix
  505. * ------ cn=accounts
  506. * ------ cn=groups
  507. * --- cn=samba
  508. * --- cn=etc
  509. * ------ cn=etc etc
  510. */
  511. static int
  512. dna_load_plugin_config()
  513. {
  514. int status = DNA_SUCCESS;
  515. int result;
  516. int i;
  517. time_t now;
  518. Slapi_PBlock *search_pb;
  519. Slapi_Entry **entries = NULL;
  520. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  521. "--> dna_load_plugin_config\n");
  522. dna_write_lock();
  523. dna_delete_config();
  524. search_pb = slapi_pblock_new();
  525. slapi_search_internal_set_pb(search_pb, getPluginDN(),
  526. LDAP_SCOPE_SUBTREE, "objectclass=*",
  527. NULL, 0, NULL, NULL, getPluginID(), 0);
  528. slapi_search_internal_pb(search_pb);
  529. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  530. if (LDAP_SUCCESS != result) {
  531. status = DNA_FAILURE;
  532. goto cleanup;
  533. }
  534. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  535. &entries);
  536. if (NULL == entries || NULL == entries[0]) {
  537. status = DNA_SUCCESS;
  538. goto cleanup;
  539. }
  540. for (i = 0; (entries[i] != NULL); i++) {
  541. /* We don't care about the status here because we may have
  542. * some invalid config entries, but we just want to continue
  543. * looking for valid ones. */
  544. dna_parse_config_entry(entries[i], 1);
  545. }
  546. /* Setup an event to update the shared config 30
  547. * seconds from now. We need to do this since
  548. * performing the operation at this point when
  549. * starting up would cause the change to not
  550. * get changelogged. */
  551. time(&now);
  552. slapi_eq_once(dna_update_config_event, NULL, now + 30);
  553. cleanup:
  554. slapi_free_search_results_internal(search_pb);
  555. slapi_pblock_destroy(search_pb);
  556. dna_unlock();
  557. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  558. "<-- dna_load_plugin_config\n");
  559. return status;
  560. }
  561. /*
  562. * dna_parse_config_entry()
  563. *
  564. * Parses a single config entry. If apply is non-zero, then
  565. * we will load and start using the new config. You can simply
  566. * validate config without making any changes by setting apply
  567. * to 0.
  568. *
  569. * Returns DNA_SUCCESS if the entry is valid and DNA_FAILURE
  570. * if it is invalid.
  571. */
  572. static int
  573. dna_parse_config_entry(Slapi_Entry * e, int apply)
  574. {
  575. char *value;
  576. struct configEntry *entry = NULL;
  577. struct configEntry *config_entry;
  578. PRCList *list;
  579. int entry_added = 0;
  580. int i = 0;
  581. int ret = DNA_SUCCESS;
  582. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  583. "--> dna_parse_config_entry\n");
  584. /* If this is the main DNA plug-in
  585. * config entry, just bail. */
  586. if (strcasecmp(getPluginDN(), slapi_entry_get_ndn(e)) == 0) {
  587. ret = DNA_FAILURE;
  588. goto bail;
  589. }
  590. entry = (struct configEntry *)
  591. slapi_ch_calloc(1, sizeof(struct configEntry));
  592. if (NULL == entry) {
  593. ret = DNA_FAILURE;
  594. goto bail;
  595. }
  596. value = slapi_entry_get_ndn(e);
  597. if (value) {
  598. entry->dn = slapi_ch_strdup(value);
  599. }
  600. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  601. "----------> dn [%s]\n", entry->dn);
  602. entry->types = slapi_entry_attr_get_charray(e, DNA_TYPE);
  603. if (entry->types == NULL) {
  604. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  605. "dna_parse_config_entry: The %s config "
  606. "setting is required for range %s.\n",
  607. DNA_TYPE, entry->dn);
  608. ret = DNA_FAILURE;
  609. goto bail;
  610. }
  611. for (i = 0; entry->types && entry->types[i]; i++) {
  612. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  613. "----------> %s [%s]\n", DNA_TYPE, entry->types[i]);
  614. }
  615. value = slapi_entry_attr_get_charptr(e, DNA_NEXTVAL);
  616. if (value) {
  617. entry->nextval = strtoull(value, 0, 0);
  618. slapi_ch_free_string(&value);
  619. } else {
  620. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  621. "dna_parse_config_entry: The %s config "
  622. "setting is required for range %s.\n",
  623. DNA_NEXTVAL, entry->dn);
  624. ret = DNA_FAILURE;
  625. goto bail;
  626. }
  627. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  628. "----------> %s [%" NSPRIu64 "]\n", DNA_NEXTVAL, entry->nextval);
  629. value = slapi_entry_attr_get_charptr(e, DNA_PREFIX);
  630. if (value && value[0]) {
  631. entry->prefix = value;
  632. } else {
  633. /* TODO - If a prefix is not defined, then we need to ensure
  634. * that the proper matching rule is in place for this
  635. * attribute type. We require this since we internally
  636. * perform a sorted range search on what we assume to
  637. * be an INTEGER syntax. */
  638. }
  639. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  640. "----------> %s [%s]\n", DNA_PREFIX, entry->prefix);
  641. /* Set the default interval to 1 */
  642. entry->interval = 1;
  643. #ifdef DNA_ENABLE_INTERVAL
  644. value = slapi_entry_attr_get_charptr(e, DNA_INTERVAL);
  645. if (value) {
  646. entry->interval = strtoull(value, 0, 0);
  647. slapi_ch_free_string(&value);
  648. }
  649. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  650. "----------> %s [%" NSPRIu64 "]\n", DNA_INTERVAL, entry->interval);
  651. #endif
  652. value = slapi_entry_attr_get_charptr(e, DNA_GENERATE);
  653. if (value) {
  654. entry->generate = value;
  655. }
  656. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  657. "----------> %s [%s]\n", DNA_GENERATE, entry->generate);
  658. value = slapi_entry_attr_get_charptr(e, DNA_FILTER);
  659. if (value) {
  660. entry->filter = value;
  661. if (NULL == (entry->slapi_filter = slapi_str2filter(value))) {
  662. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM ,
  663. "Error: Invalid search filter in entry [%s]: [%s]\n",
  664. entry->dn, value);
  665. ret = DNA_FAILURE;
  666. goto bail;
  667. }
  668. } else {
  669. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  670. "dna_parse_config_entry: The %s config "
  671. "setting is required for range %s.\n",
  672. DNA_FILTER, entry->dn);
  673. ret = DNA_FAILURE;
  674. goto bail;
  675. }
  676. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  677. "----------> %s [%s]\n", DNA_FILTER, value);
  678. value = slapi_entry_attr_get_charptr(e, DNA_SCOPE);
  679. if (value) {
  680. /* TODO - Allow multiple scope settings for a single range. This may
  681. * make ordering the scopes tough when we put them in the clist. */
  682. entry->scope = value;
  683. } else {
  684. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  685. "dna_parse_config_entry: The %s config "
  686. "config setting is required for range %s.\n",
  687. DNA_SCOPE, entry->dn);
  688. ret = DNA_FAILURE;
  689. goto bail;
  690. }
  691. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  692. "----------> %s [%s]\n", DNA_SCOPE, entry->scope);
  693. /* optional, if not specified set -1 which is converted to the max unisgnee
  694. * value */
  695. value = slapi_entry_attr_get_charptr(e, DNA_MAXVAL);
  696. if (value) {
  697. entry->maxval = strtoull(value, 0, 0);
  698. slapi_ch_free_string(&value);
  699. } else {
  700. entry->maxval = -1;
  701. }
  702. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  703. "----------> %s [%" NSPRIu64 "]\n", DNA_MAXVAL, entry->maxval);
  704. value = slapi_entry_attr_get_charptr(e, DNA_SHARED_CFG_DN);
  705. if (value) {
  706. Slapi_Entry *shared_e = NULL;
  707. Slapi_DN *sdn = NULL;
  708. char *normdn = NULL;
  709. sdn = slapi_sdn_new_dn_byref(value);
  710. if (sdn) {
  711. slapi_search_internal_get_entry(sdn, NULL, &shared_e, getPluginID());
  712. slapi_sdn_free(&sdn);
  713. }
  714. /* Make sure that the shared config entry exists. */
  715. if (!shared_e) {
  716. /* We didn't locate the shared config container entry. Log
  717. * a message and skip this config entry. */
  718. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  719. "dna_parse_config_entry: Unable to locate "
  720. "shared configuration entry (%s)\n", value);
  721. ret = DNA_FAILURE;
  722. slapi_ch_free_string(&value);
  723. goto bail;
  724. } else {
  725. slapi_entry_free(shared_e);
  726. shared_e = NULL;
  727. }
  728. normdn = slapi_create_dn_string("%s", value);
  729. if (NULL == normdn) {
  730. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  731. "dna_parse_config_entry: failed to normalize dn: "
  732. "%s\n", value);
  733. ret = DNA_FAILURE;
  734. goto bail;
  735. }
  736. entry->shared_cfg_base = normdn;
  737. /* We prepend the host & port of this instance as a
  738. * multi-part RDN for the shared config entry. */
  739. normdn = slapi_create_dn_string("%s=%s+%s=%s,%s", DNA_HOSTNAME,
  740. hostname, DNA_PORTNUM, portnum, normdn);
  741. if (NULL == normdn) {
  742. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  743. "dna_parse_config_entry: failed to create dn: "
  744. "%s=%s+%s=%s,%s", DNA_HOSTNAME,
  745. hostname, DNA_PORTNUM, portnum, value);
  746. ret = DNA_FAILURE;
  747. goto bail;
  748. }
  749. entry->shared_cfg_dn = normdn;
  750. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  751. "----------> %s [%s]\n", DNA_SHARED_CFG_DN,
  752. entry->shared_cfg_base);
  753. }
  754. value = slapi_entry_attr_get_charptr(e, DNA_THRESHOLD);
  755. if (value) {
  756. entry->threshold = strtoull(value, 0, 0);
  757. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  758. "----------> %s [%s]\n", DNA_THRESHOLD, value);
  759. slapi_ch_free_string(&value);
  760. } else {
  761. entry->threshold = 1;
  762. }
  763. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  764. "----------> %s [%" NSPRIu64 "]\n", DNA_THRESHOLD, entry->threshold);
  765. value = slapi_entry_attr_get_charptr(e, DNA_RANGE_REQUEST_TIMEOUT);
  766. if (value) {
  767. entry->timeout = strtoull(value, 0, 0);
  768. slapi_ch_free_string(&value);
  769. } else {
  770. entry->timeout = DNA_DEFAULT_TIMEOUT;
  771. }
  772. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  773. "----------> %s [%" NSPRIu64 "]\n", DNA_RANGE_REQUEST_TIMEOUT,
  774. entry->timeout);
  775. value = slapi_entry_attr_get_charptr(e, DNA_NEXT_RANGE);
  776. if (value) {
  777. char *p = NULL;
  778. /* the next range value is in the form "<lower>-<upper>" */
  779. if ((p = strstr(value, "-")) != NULL) {
  780. *p = '\0';
  781. ++p;
  782. entry->next_range_lower = strtoull(value, 0, 0);
  783. entry->next_range_upper = strtoull(p, 0, 0);
  784. /* validate that upper is greater than lower */
  785. if (entry->next_range_upper <= entry->next_range_lower) {
  786. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  787. "dna_parse_config_entry: Illegal %s "
  788. "setting specified for range %s. Legal "
  789. "format is <lower>-<upper>.\n",
  790. DNA_NEXT_RANGE, entry->dn);
  791. ret = DNA_FAILURE;
  792. entry->next_range_lower = 0;
  793. entry->next_range_upper = 0;
  794. }
  795. /* make sure next range doesn't overlap with
  796. * the active range */
  797. if (((entry->next_range_upper <= entry->maxval) &&
  798. (entry->next_range_upper >= entry->nextval)) ||
  799. ((entry->next_range_lower <= entry->maxval) &&
  800. (entry->next_range_lower >= entry->nextval))) {
  801. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  802. "dna_parse_config_entry: Illegal %s "
  803. "setting specified for range %s. %s "
  804. "overlaps with the active range.\n",
  805. DNA_NEXT_RANGE, entry->dn, DNA_NEXT_RANGE);
  806. ret = DNA_FAILURE;
  807. entry->next_range_lower = 0;
  808. entry->next_range_upper = 0;
  809. }
  810. } else {
  811. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  812. "dna_parse_config_entry: Illegal %s "
  813. "setting specified for range %s. Legal "
  814. "format is <lower>-<upper>.\n",
  815. DNA_NEXT_RANGE, entry->dn);
  816. ret = DNA_FAILURE;
  817. }
  818. slapi_ch_free_string(&value);
  819. }
  820. /* If we were only called to validate config, we can
  821. * just bail out before applying the config changes */
  822. if (apply == 0) {
  823. goto bail;
  824. }
  825. /* Calculate number of remaining values. */
  826. if (entry->next_range_lower != 0) {
  827. entry->remaining = ((entry->next_range_upper - entry->next_range_lower + 1) /
  828. entry->interval) + ((entry->maxval - entry->nextval + 1) /
  829. entry->interval);
  830. } else if (entry->nextval >= entry->maxval) {
  831. entry->remaining = 0;
  832. } else {
  833. entry->remaining = ((entry->maxval - entry->nextval + 1) /
  834. entry->interval);
  835. }
  836. /* create the new value lock for this range */
  837. entry->lock = slapi_new_mutex();
  838. if (!entry->lock) {
  839. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  840. "dna_parse_config_entry: Unable to create lock "
  841. "for range %s.\n", entry->dn);
  842. ret = DNA_FAILURE;
  843. goto bail;
  844. }
  845. /**
  846. * Finally add the entry to the list.
  847. * We sort by scope dn length with longer
  848. * dn's first - this allows the scope
  849. * checking code to be simple and quick and
  850. * cunningly linear.
  851. */
  852. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  853. list = PR_LIST_HEAD(dna_global_config);
  854. while (list != dna_global_config) {
  855. config_entry = (struct configEntry *) list;
  856. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  857. PR_INSERT_BEFORE(&(entry->list), list);
  858. slapi_log_error(SLAPI_LOG_CONFIG,
  859. DNA_PLUGIN_SUBSYSTEM,
  860. "store [%s] before [%s] \n", entry->scope,
  861. config_entry->scope);
  862. entry_added = 1;
  863. break;
  864. }
  865. next:
  866. list = PR_NEXT_LINK(list);
  867. if (dna_global_config == list) {
  868. /* add to tail */
  869. PR_INSERT_BEFORE(&(entry->list), list);
  870. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  871. "store [%s] at tail\n", entry->scope);
  872. entry_added = 1;
  873. break;
  874. }
  875. }
  876. } else {
  877. /* first entry */
  878. PR_INSERT_LINK(&(entry->list), dna_global_config);
  879. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  880. "store [%s] at head \n", entry->scope);
  881. entry_added = 1;
  882. }
  883. bail:
  884. if (0 == entry_added) {
  885. /* Don't log error if we weren't asked to apply config */
  886. if ((apply != 0) && (entry != NULL)) {
  887. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  888. "dna_parse_config_entry: Invalid config entry "
  889. "[%s] skipped\n", entry->dn);
  890. }
  891. dna_free_config_entry(&entry);
  892. } else {
  893. ret = DNA_SUCCESS;
  894. }
  895. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  896. "<-- dna_parse_config_entry\n");
  897. return ret;
  898. }
  899. static void
  900. dna_free_config_entry(struct configEntry ** entry)
  901. {
  902. struct configEntry *e = *entry;
  903. if (e == NULL)
  904. return;
  905. if (e->dn) {
  906. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  907. "freeing config entry [%s]\n", e->dn);
  908. slapi_ch_free_string(&e->dn);
  909. }
  910. if (e->types)
  911. slapi_ch_array_free(e->types);
  912. if (e->prefix)
  913. slapi_ch_free_string(&e->prefix);
  914. if (e->filter)
  915. slapi_ch_free_string(&e->filter);
  916. if (e->slapi_filter)
  917. slapi_filter_free(e->slapi_filter, 1);
  918. if (e->generate)
  919. slapi_ch_free_string(&e->generate);
  920. if (e->scope)
  921. slapi_ch_free_string(&e->scope);
  922. if (e->shared_cfg_base)
  923. slapi_ch_free_string(&e->shared_cfg_base);
  924. if (e->shared_cfg_dn)
  925. slapi_ch_free_string(&e->shared_cfg_dn);
  926. if (e->lock)
  927. slapi_destroy_mutex(e->lock);
  928. slapi_ch_free((void **) entry);
  929. }
  930. static void
  931. dna_delete_configEntry(PRCList *entry)
  932. {
  933. PR_REMOVE_LINK(entry);
  934. dna_free_config_entry((struct configEntry **) &entry);
  935. }
  936. static void
  937. dna_delete_config()
  938. {
  939. PRCList *list;
  940. while (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  941. list = PR_LIST_HEAD(dna_global_config);
  942. dna_delete_configEntry(list);
  943. }
  944. return;
  945. }
  946. static void
  947. dna_free_shared_server(struct dnaServer **server)
  948. {
  949. struct dnaServer *s = *server;
  950. slapi_ch_free_string(&s->host);
  951. slapi_ch_free((void **)server);
  952. }
  953. static void
  954. dna_delete_shared_servers(PRCList **servers)
  955. {
  956. PRCList *server;
  957. while (!PR_CLIST_IS_EMPTY(*servers)) {
  958. server = PR_LIST_HEAD(*servers);
  959. PR_REMOVE_LINK(server);
  960. dna_free_shared_server((struct dnaServer **)&server);
  961. }
  962. slapi_ch_free((void **)servers);
  963. *servers = NULL;
  964. return;
  965. }
  966. static int
  967. dna_load_host_port()
  968. {
  969. int status = DNA_SUCCESS;
  970. Slapi_Entry *e = NULL;
  971. Slapi_DN *config_dn = NULL;
  972. char *attrs[4];
  973. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  974. "--> dna_load_host_port\n");
  975. attrs[0] = "nsslapd-localhost";
  976. attrs[1] = "nsslapd-port";
  977. attrs[2] = "nsslapd-secureport";
  978. attrs[3] = NULL;
  979. config_dn = slapi_sdn_new_dn_byref("cn=config");
  980. if (config_dn) {
  981. slapi_search_internal_get_entry(config_dn, attrs, &e, getPluginID());
  982. slapi_sdn_free(&config_dn);
  983. }
  984. if (e) {
  985. hostname = slapi_entry_attr_get_charptr(e, "nsslapd-localhost");
  986. portnum = slapi_entry_attr_get_charptr(e, "nsslapd-port");
  987. secureportnum = slapi_entry_attr_get_charptr(e, "nsslapd-secureport");
  988. slapi_entry_free(e);
  989. }
  990. if (!hostname || !portnum) {
  991. status = DNA_FAILURE;
  992. }
  993. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  994. "<-- dna_load_host_port\n");
  995. return status;
  996. }
  997. /*
  998. * dna_update_config_event()
  999. *
  1000. * Event queue callback that we use to do the initial
  1001. * update of the shared config entries shortly after
  1002. * startup.
  1003. */
  1004. static void
  1005. dna_update_config_event(time_t event_time, void *arg)
  1006. {
  1007. Slapi_PBlock *pb = NULL;
  1008. struct configEntry *config_entry = NULL;
  1009. PRCList *list = NULL;
  1010. /* Get read lock to prevent config changes */
  1011. dna_read_lock();
  1012. /* Loop through all config entries and update the shared
  1013. * config entries. */
  1014. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  1015. list = PR_LIST_HEAD(dna_global_config);
  1016. /* Create the pblock. We'll reuse this for all
  1017. * shared config updates. */
  1018. if ((pb = slapi_pblock_new()) == NULL)
  1019. goto bail;
  1020. while (list != dna_global_config) {
  1021. config_entry = (struct configEntry *) list;
  1022. /* If a shared config dn is set, update the shared config. */
  1023. if (config_entry->shared_cfg_dn != NULL) {
  1024. slapi_lock_mutex(config_entry->lock);
  1025. /* First delete the existing shared config entry. This
  1026. * will allow the entry to be updated for things like
  1027. * port number changes, etc. */
  1028. slapi_delete_internal_set_pb(pb, config_entry->shared_cfg_dn,
  1029. NULL, NULL, getPluginID(), 0);
  1030. /* We don't care about the results */
  1031. slapi_delete_internal_pb(pb);
  1032. /* Now force the entry to be recreated */
  1033. dna_update_shared_config(config_entry);
  1034. slapi_unlock_mutex(config_entry->lock);
  1035. slapi_pblock_init(pb);
  1036. }
  1037. list = PR_NEXT_LINK(list);
  1038. }
  1039. }
  1040. bail:
  1041. dna_unlock();
  1042. slapi_pblock_destroy(pb);
  1043. }
  1044. /****************************************************
  1045. Distributed ranges Helpers
  1046. ****************************************************/
  1047. /*
  1048. * dna_fix_maxval()
  1049. *
  1050. * Attempts to extend the range represented by
  1051. * config_entry.
  1052. *
  1053. * The lock for configEntry should be obtained
  1054. * before calling this function.
  1055. */
  1056. static int dna_fix_maxval(struct configEntry *config_entry)
  1057. {
  1058. PRCList *servers = NULL;
  1059. PRCList *server = NULL;
  1060. PRUint64 lower = 0;
  1061. PRUint64 upper = 0;
  1062. int ret = LDAP_OPERATIONS_ERROR;
  1063. if (config_entry == NULL) {
  1064. goto bail;
  1065. }
  1066. /* If we already have a next range we only need
  1067. * to activate it. */
  1068. if (config_entry->next_range_lower != 0) {
  1069. ret = dna_activate_next_range(config_entry);
  1070. if (ret != 0) {
  1071. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1072. "dna_fix_maxval: Unable to activate the "
  1073. "next range for range %s.\n", config_entry->dn);
  1074. }
  1075. } else if (config_entry->shared_cfg_base) {
  1076. /* Find out if there are any other servers to request
  1077. * range from. */
  1078. dna_get_shared_servers(config_entry, &servers);
  1079. if (servers) {
  1080. /* We have other servers we can try to extend
  1081. * our range from. Loop through them and
  1082. * request range until someone gives us some
  1083. * values, or we hit the end of the list. */
  1084. server = PR_LIST_HEAD(servers);
  1085. while (server != servers) {
  1086. if (dna_request_range(config_entry, (struct dnaServer *)server,
  1087. &lower, &upper) != 0) {
  1088. server = PR_NEXT_LINK(server);
  1089. } else {
  1090. /* Someone provided us with a new range. Attempt
  1091. * to update the config. */
  1092. if ((ret = dna_update_next_range(config_entry, lower, upper)) == 0) {
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. /* free the list of servers */
  1098. dna_delete_shared_servers(&servers);
  1099. }
  1100. }
  1101. bail:
  1102. return ret;
  1103. }
  1104. /* dna_notice_allocation()
  1105. *
  1106. * Called after a new value has been allocated from the range.
  1107. * This function will update the config entries and cached info
  1108. * appropriately. This includes activating the next range if
  1109. * we've exhausted the current range.
  1110. *
  1111. * The last parameter is the value that has just been allocated.
  1112. * The new parameter should be the next available value. If you
  1113. * set both of these parameters to 0, then this function will
  1114. * just check if the next range needs to be activated and update
  1115. * the config accordingly.
  1116. *
  1117. * The lock for configEntry should be obtained before calling
  1118. * this function. */
  1119. static void
  1120. dna_notice_allocation(struct configEntry *config_entry, PRUint64 new,
  1121. PRUint64 last, int fix)
  1122. {
  1123. /* update our cached config entry */
  1124. if ((new != 0) && (new <= (config_entry->maxval + config_entry->interval))) {
  1125. config_entry->nextval = new;
  1126. }
  1127. /* check if we've exhausted our active range */
  1128. if ((last == config_entry->maxval) ||
  1129. (config_entry->nextval > config_entry->maxval)) {
  1130. /* If we already have a next range set, make it the
  1131. * new active range. */
  1132. if (config_entry->next_range_lower != 0) {
  1133. /* Make the next range active */
  1134. if (dna_activate_next_range(config_entry) != 0) {
  1135. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1136. "dna_notice_allocation: Unable to activate "
  1137. "the next range for range %s.\n", config_entry->dn);
  1138. }
  1139. } else {
  1140. config_entry->remaining = 0;
  1141. /* update the shared configuration */
  1142. dna_update_shared_config(config_entry);
  1143. }
  1144. } else {
  1145. if (config_entry->next_range_lower != 0) {
  1146. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1147. config_entry->interval) + ((config_entry->next_range_upper -
  1148. config_entry->next_range_lower +1) / config_entry->interval);
  1149. } else {
  1150. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1151. config_entry->interval);
  1152. }
  1153. /* update the shared configuration */
  1154. dna_update_shared_config(config_entry);
  1155. }
  1156. /* Check if we passed the threshold and try to fix maxval if so. We
  1157. * don't need to do this if we already have a next range on deck. */
  1158. if ((config_entry->next_range_lower == 0) && (config_entry->remaining <= config_entry->threshold)) {
  1159. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1160. "dna_notice_allocation: Passed threshold of %" NSPRIu64 " remaining values "
  1161. "for range %s. (%" NSPRIu64 " values remain)\n",
  1162. config_entry->threshold, config_entry->dn, config_entry->remaining);
  1163. /* Only attempt to fix maxval if the fix flag is set. */
  1164. if (fix != 0) {
  1165. dna_fix_maxval(config_entry);
  1166. }
  1167. }
  1168. return;
  1169. }
  1170. static int
  1171. dna_get_shared_servers(struct configEntry *config_entry, PRCList **servers)
  1172. {
  1173. int ret = LDAP_SUCCESS;
  1174. Slapi_PBlock *pb = NULL;
  1175. Slapi_Entry **entries = NULL;
  1176. char *attrs[5];
  1177. /* First do a search in the shared config area for this
  1178. * range to find other servers who are managing this range. */
  1179. attrs[0] = DNA_HOSTNAME;
  1180. attrs[1] = DNA_PORTNUM;
  1181. attrs[2] = DNA_SECURE_PORTNUM;
  1182. attrs[3] = DNA_REMAINING;
  1183. attrs[4] = NULL;
  1184. pb = slapi_pblock_new();
  1185. if (NULL == pb) {
  1186. ret = LDAP_OPERATIONS_ERROR;
  1187. goto cleanup;
  1188. }
  1189. slapi_search_internal_set_pb(pb, config_entry->shared_cfg_base,
  1190. LDAP_SCOPE_ONELEVEL, "objectclass=*",
  1191. attrs, 0, NULL,
  1192. NULL, getPluginID(), 0);
  1193. slapi_search_internal_pb(pb);
  1194. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1195. if (LDAP_SUCCESS != ret) {
  1196. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1197. "dna_get_shared_servers: search failed for shared "
  1198. "config: %s [error %d]\n", config_entry->shared_cfg_base,
  1199. ret);
  1200. goto cleanup;
  1201. }
  1202. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1203. &entries);
  1204. if (entries && entries[0]) {
  1205. Slapi_DN *cfg_sdn = NULL;
  1206. int i;
  1207. cfg_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_dn);
  1208. /* We found some entries. Go through them and
  1209. * order them based off of remaining values. */
  1210. for (i = 0; entries[i]; i++) {
  1211. /* skip our own shared config entry */
  1212. if (slapi_sdn_compare(cfg_sdn, slapi_entry_get_sdn(entries[i]))) {
  1213. struct dnaServer *server = NULL;
  1214. /* set up the server list entry */
  1215. server = (struct dnaServer *) slapi_ch_calloc(1,
  1216. sizeof(struct dnaServer));
  1217. server->host = slapi_entry_attr_get_charptr(entries[i],
  1218. DNA_HOSTNAME);
  1219. server->port = slapi_entry_attr_get_uint(entries[i], DNA_PORTNUM);
  1220. server->secureport = slapi_entry_attr_get_uint(entries[i], DNA_SECURE_PORTNUM);
  1221. server->remaining = slapi_entry_attr_get_ulonglong(entries[i],
  1222. DNA_REMAINING);
  1223. /* validate the entry */
  1224. if (!server->host || server->port == 0 || server->remaining == 0) {
  1225. /* free and skip this one */
  1226. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1227. "dna_get_shared_servers: skipping invalid "
  1228. "shared config entry (%s)\n", slapi_entry_get_dn(entries[i]));
  1229. dna_free_shared_server(&server);
  1230. continue;
  1231. }
  1232. /* add a server entry to the list */
  1233. if (*servers == NULL) {
  1234. /* first entry */
  1235. *servers = (PRCList *) slapi_ch_calloc(1,
  1236. sizeof(struct dnaServer));
  1237. PR_INIT_CLIST(*servers);
  1238. PR_INSERT_LINK(&(server->list), *servers);
  1239. } else {
  1240. /* Find the right slot for this entry. We
  1241. * want to order the entries based off of
  1242. * the remaining number of values, higest
  1243. * to lowest. */
  1244. struct dnaServer *sitem;
  1245. PRCList* item = PR_LIST_HEAD(*servers);
  1246. while (item != *servers) {
  1247. sitem = (struct dnaServer *)item;
  1248. if (server->remaining > sitem->remaining) {
  1249. PR_INSERT_BEFORE(&(server->list), item);
  1250. break;
  1251. }
  1252. item = PR_NEXT_LINK(item);
  1253. if (*servers == item) {
  1254. /* add to tail */
  1255. PR_INSERT_BEFORE(&(server->list), item);
  1256. break;
  1257. }
  1258. }
  1259. }
  1260. }
  1261. }
  1262. slapi_sdn_free(&cfg_sdn);
  1263. }
  1264. cleanup:
  1265. slapi_free_search_results_internal(pb);
  1266. slapi_pblock_destroy(pb);
  1267. return ret;
  1268. }
  1269. /*
  1270. * dna_request_range()
  1271. *
  1272. * Requests range extension from another server.
  1273. * Returns 0 on success and will fill in upper
  1274. * and lower. Returns non-0 on failure and will
  1275. * zero out upper and lower.
  1276. */
  1277. static int dna_request_range(struct configEntry *config_entry,
  1278. struct dnaServer *server,
  1279. PRUint64 *lower, PRUint64 *upper)
  1280. {
  1281. char *bind_dn = NULL;
  1282. char *bind_passwd = NULL;
  1283. char *bind_method = NULL;
  1284. int is_ssl = 0;
  1285. struct berval *request = NULL;
  1286. char *retoid = NULL;
  1287. struct berval *responsedata = NULL;
  1288. BerElement *respber = NULL;
  1289. LDAP *ld = NULL;
  1290. char *lower_str = NULL;
  1291. char *upper_str = NULL;
  1292. int set_extend_flag = 0;
  1293. int ret = LDAP_OPERATIONS_ERROR;
  1294. int port = 0;
  1295. int timelimit;
  1296. #if defined(USE_OPENLDAP)
  1297. struct timeval timeout;
  1298. #endif
  1299. /* See if we're allowed to send a range request now */
  1300. slapi_lock_mutex(config_entry->extend_lock);
  1301. if (config_entry->extend_in_progress) {
  1302. /* We're already processing a range extention, so bail. */
  1303. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1304. "dna_request_range: Already processing a "
  1305. "range extension request. Skipping request.\n");
  1306. slapi_unlock_mutex(config_entry->extend_lock);
  1307. goto bail;
  1308. } else {
  1309. /* Set a flag indicating that we're attempting to extend this range */
  1310. config_entry->extend_in_progress = 1;
  1311. set_extend_flag = 1;
  1312. slapi_unlock_mutex(config_entry->extend_lock);
  1313. }
  1314. /* Fetch the replication bind dn info */
  1315. if (dna_get_replica_bind_creds(config_entry->shared_cfg_base, server,
  1316. &bind_dn, &bind_passwd, &bind_method,
  1317. &is_ssl, &port) != 0) {
  1318. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1319. "dna_request_range: Unable to retrieve "
  1320. "replica bind credentials.\n");
  1321. goto bail;
  1322. }
  1323. if ((request = dna_create_range_request(config_entry->shared_cfg_base)) == NULL) {
  1324. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1325. "dna_request_range: Failed to create "
  1326. "range extension extended operation request.\n");
  1327. goto bail;
  1328. }
  1329. if ((ld = slapi_ldap_init(server->host, port, is_ssl, 0)) == NULL) {
  1330. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1331. "dna_request_range: Unable to "
  1332. "initialize LDAP session to server %s:%u.\n",
  1333. server->host, server->port);
  1334. goto bail;
  1335. }
  1336. /* Disable referrals and set timelimit and a connect timeout */
  1337. ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1338. timelimit = config_entry->timeout / 1000; /* timeout is in msec */
  1339. ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &timelimit);
  1340. #if defined(USE_OPENLDAP)
  1341. timeout.tv_sec = config_entry->timeout / 1000;
  1342. timeout.tv_usec = (config_entry->timeout % 1000) * 1000;
  1343. ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &timeout);
  1344. #else
  1345. ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, &config_entry->timeout);
  1346. #endif
  1347. /* Bind to the replica server */
  1348. ret = slapi_ldap_bind(ld, bind_dn, bind_passwd, bind_method,
  1349. NULL, NULL, NULL, NULL);
  1350. if (ret != LDAP_SUCCESS) {
  1351. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1352. "dna_request_range: Error binding "
  1353. " to replica server %s:%u. [error %d]\n",
  1354. server->host, server->port, ret);
  1355. goto bail;
  1356. }
  1357. /* Send range extension request */
  1358. ret = ldap_extended_operation_s(ld, DNA_EXTEND_EXOP_REQUEST_OID,
  1359. request, NULL, NULL, &retoid, &responsedata);
  1360. if (ret != LDAP_SUCCESS) {
  1361. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1362. "dna_request_range: Error sending "
  1363. "range extension extended operation request "
  1364. "to server %s:%u [error %d]\n", server->host,
  1365. server->port, ret);
  1366. goto bail;
  1367. }
  1368. /* Verify that the OID is correct. */
  1369. if (strcmp(retoid, DNA_EXTEND_EXOP_RESPONSE_OID) != 0) {
  1370. ret = LDAP_OPERATIONS_ERROR;
  1371. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1372. "dna_request_range: Received incorrect response OID.\n");
  1373. goto bail;
  1374. }
  1375. /* Parse response */
  1376. if (responsedata) {
  1377. respber = ber_init(responsedata);
  1378. ber_scanf(respber, "{aa}", &lower_str, &upper_str);
  1379. }
  1380. /* Fill in upper and lower */
  1381. if (upper_str && lower_str) {
  1382. *upper = strtoull(upper_str, 0, 0);
  1383. *lower = strtoull(lower_str, 0, 0);
  1384. ret = 0;
  1385. } else {
  1386. ret = LDAP_OPERATIONS_ERROR;
  1387. }
  1388. bail:
  1389. if (set_extend_flag) {
  1390. slapi_lock_mutex(config_entry->extend_lock);
  1391. config_entry->extend_in_progress = 0;
  1392. slapi_unlock_mutex(config_entry->extend_lock);
  1393. }
  1394. slapi_ldap_unbind(ld);
  1395. slapi_ch_free_string(&bind_dn);
  1396. slapi_ch_free_string(&bind_passwd);
  1397. slapi_ch_free_string(&bind_method);
  1398. slapi_ch_free_string(&retoid);
  1399. slapi_ch_free_string(&lower_str);
  1400. slapi_ch_free_string(&upper_str);
  1401. ber_free(respber, 1);
  1402. ber_bvfree(request);
  1403. ber_bvfree(responsedata);
  1404. if (ret != 0) {
  1405. *upper = 0;
  1406. *lower = 0;
  1407. }
  1408. return ret;
  1409. }
  1410. static struct berval *dna_create_range_request(char *range_dn)
  1411. {
  1412. struct berval *requestdata = NULL;
  1413. struct berval shared_dn = { 0, NULL };
  1414. BerElement *ber = NULL;
  1415. shared_dn.bv_val = range_dn;
  1416. shared_dn.bv_len = strlen(shared_dn.bv_val);
  1417. if((ber = ber_alloc()) == NULL) {
  1418. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1419. "dna_create_range_request: Error "
  1420. "allocating request data.\n");
  1421. goto bail;
  1422. }
  1423. if (LBER_ERROR == (ber_printf(ber, "{o}", shared_dn.bv_val, shared_dn.bv_len))) {
  1424. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1425. "dna_create_range_request: Error "
  1426. "encoding request data.\n");
  1427. goto bail;
  1428. }
  1429. if (ber_flatten(ber, &requestdata) == -1) {
  1430. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1431. "dna_create_range_request: Error "
  1432. "encoding request data.\n");
  1433. goto bail;
  1434. }
  1435. bail:
  1436. ber_free(ber, 1);
  1437. return requestdata;
  1438. }
  1439. /****************************************************
  1440. Helpers
  1441. ****************************************************/
  1442. static char *dna_get_dn(Slapi_PBlock * pb)
  1443. {
  1444. char *dn = 0;
  1445. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1446. "--> dna_get_dn\n");
  1447. if (slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn)) {
  1448. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1449. "dna_get_dn: failed to get dn of changed entry");
  1450. goto bail;
  1451. }
  1452. bail:
  1453. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1454. "<-- dna_get_dn\n");
  1455. return dn;
  1456. }
  1457. /* config check
  1458. matching config dn or a descendent reloads config
  1459. */
  1460. static int dna_dn_is_config(char *dn)
  1461. {
  1462. int ret = 0;
  1463. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1464. "--> dna_is_config\n");
  1465. if (slapi_dn_issuffix(dn, getPluginDN())) {
  1466. ret = 1;
  1467. }
  1468. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1469. "<-- dna_is_config\n");
  1470. return ret;
  1471. }
  1472. #define DNA_LDAP_TAG_SK_REVERSE 0x81L
  1473. static LDAPControl *dna_build_sort_control(const char *attr)
  1474. {
  1475. LDAPControl *ctrl;
  1476. BerElement *ber;
  1477. int rc;
  1478. ber = ber_alloc();
  1479. if (NULL == ber)
  1480. return NULL;
  1481. rc = ber_printf(ber, "{{stb}}", attr, DNA_LDAP_TAG_SK_REVERSE, 1);
  1482. if (-1 == rc) {
  1483. ber_free(ber, 1);
  1484. return NULL;
  1485. }
  1486. rc = slapi_build_control(LDAP_CONTROL_SORTREQUEST, ber, 1, &ctrl);
  1487. ber_free(ber, 1);
  1488. if (LDAP_SUCCESS != rc)
  1489. return NULL;
  1490. return ctrl;
  1491. }
  1492. /****************************************************
  1493. Functions that actually do things other
  1494. than config and startup
  1495. ****************************************************/
  1496. /*
  1497. * dna_first_free_value()
  1498. *
  1499. * We do search all values between nextval and maxval asking the
  1500. * server to sort them, then we check the first free spot and
  1501. * use it as newval. If we go past the end of the range, we
  1502. * return LDAP_OPERATIONS_ERROR and set newval to be > the
  1503. * maximum configured value for this range. */
  1504. static int
  1505. dna_first_free_value(struct configEntry *config_entry,
  1506. PRUint64 *newval)
  1507. {
  1508. Slapi_Entry **entries = NULL;
  1509. Slapi_PBlock *pb = NULL;
  1510. LDAPControl **ctrls = NULL;
  1511. char *filter = NULL;
  1512. char *prefix;
  1513. int multitype;
  1514. int result, status, filterlen;
  1515. PRUint64 tmpval, sval, i;
  1516. char *strval = NULL;
  1517. /* check if the config is already out of range */
  1518. if (config_entry->nextval > config_entry->maxval) {
  1519. *newval = config_entry->nextval;
  1520. return LDAP_OPERATIONS_ERROR;
  1521. }
  1522. prefix = config_entry->prefix;
  1523. tmpval = config_entry->nextval;
  1524. if (dna_is_multitype_range(config_entry)) {
  1525. multitype = 1;
  1526. }
  1527. /* We don't sort if we're using a prefix (non integer type) or if this
  1528. * is a multi-type range. Instead, we just search to see if the next
  1529. * value is free, and keep incrementing until we find the next free value. */
  1530. if (prefix || multitype) {
  1531. /* This will allocate the filter string for us. */
  1532. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1533. } else {
  1534. /* This is a single-type range, so just use the first (only)
  1535. * type from the list. */
  1536. ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl));
  1537. if (NULL == ctrls)
  1538. return LDAP_OPERATIONS_ERROR;
  1539. ctrls[0] = dna_build_sort_control(config_entry->types[0]);
  1540. if (NULL == ctrls[0]) {
  1541. slapi_ch_free((void **)&ctrls);
  1542. return LDAP_OPERATIONS_ERROR;
  1543. }
  1544. filter = slapi_ch_smprintf("(&%s(&(%s>=%" NSPRIu64 ")(%s<=%" NSPRIu64 ")))",
  1545. config_entry->filter,
  1546. config_entry->types[0], tmpval,
  1547. config_entry->types[0], config_entry->maxval);
  1548. }
  1549. if (NULL == filter) {
  1550. ldap_controls_free(ctrls);
  1551. ctrls = NULL;
  1552. return LDAP_OPERATIONS_ERROR;
  1553. }
  1554. pb = slapi_pblock_new();
  1555. if (NULL == pb) {
  1556. ldap_controls_free(ctrls);
  1557. ctrls = NULL;
  1558. slapi_ch_free_string(&filter);
  1559. return LDAP_OPERATIONS_ERROR;
  1560. }
  1561. slapi_search_internal_set_pb(pb, config_entry->scope,
  1562. LDAP_SCOPE_SUBTREE, filter,
  1563. config_entry->types, 0, ctrls,
  1564. NULL, getPluginID(), 0);
  1565. slapi_search_internal_pb(pb);
  1566. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1567. if (LDAP_SUCCESS != result) {
  1568. status = LDAP_OPERATIONS_ERROR;
  1569. goto cleanup;
  1570. }
  1571. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1572. &entries);
  1573. if (NULL == entries || NULL == entries[0]) {
  1574. /* no values means we already have a good value */
  1575. *newval = tmpval;
  1576. status = LDAP_SUCCESS;
  1577. goto cleanup;
  1578. }
  1579. if (prefix || multitype) {
  1580. /* The next value identified in the config entry has already
  1581. * been taken. We just iterate through the values until we
  1582. * (hopefully) find a free one. */
  1583. for (tmpval += config_entry->interval; tmpval <= config_entry->maxval;
  1584. tmpval += config_entry->interval) {
  1585. /* This will reuse the old memory for the previous filter. It is
  1586. * guaranteed to have enough space since the filter is the same
  1587. * aside from the assertion value (we allocated enough for the
  1588. * largest supported integer). */
  1589. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1590. /* clear out the pblock so we can re-use it */
  1591. slapi_free_search_results_internal(pb);
  1592. slapi_pblock_init(pb);
  1593. slapi_search_internal_set_pb(pb, config_entry->scope,
  1594. LDAP_SCOPE_SUBTREE, filter,
  1595. config_entry->types, 0, 0,
  1596. NULL, getPluginID(), 0);
  1597. slapi_search_internal_pb(pb);
  1598. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1599. if (LDAP_SUCCESS != result) {
  1600. status = LDAP_OPERATIONS_ERROR;
  1601. goto cleanup;
  1602. }
  1603. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1604. &entries);
  1605. if (NULL == entries || NULL == entries[0]) {
  1606. /* no values means we already have a good value */
  1607. *newval = tmpval;
  1608. status = LDAP_SUCCESS;
  1609. goto cleanup;
  1610. }
  1611. }
  1612. } else {
  1613. /* Entries are sorted and filtered for value >= tval therefore if the
  1614. * first one does not match tval it means that the value is free,
  1615. * otherwise we need to cycle through values until we find a mismatch,
  1616. * the first mismatch is the first free pit. This is guaranteed to
  1617. * be a single-type range, so we can just use the first (only)
  1618. * type from the list of types directly. */
  1619. sval = 0;
  1620. for (i = 0; NULL != entries[i]; i++) {
  1621. strval = slapi_entry_attr_get_charptr(entries[i], config_entry->types[0]);
  1622. errno = 0;
  1623. sval = strtoull(strval, 0, 0);
  1624. if (errno) {
  1625. /* something very wrong here ... */
  1626. status = LDAP_OPERATIONS_ERROR;
  1627. goto cleanup;
  1628. }
  1629. slapi_ch_free_string(&strval);
  1630. if (tmpval != sval)
  1631. break;
  1632. if (config_entry->maxval < sval)
  1633. break;
  1634. tmpval += config_entry->interval;
  1635. }
  1636. }
  1637. /* check if we went past the end of the range */
  1638. if (tmpval <= config_entry->maxval) {
  1639. *newval = tmpval;
  1640. status = LDAP_SUCCESS;
  1641. } else {
  1642. /* we set newval past the end of the range
  1643. * so the caller can easily detect that we
  1644. * overflowed the configured range. */
  1645. *newval = tmpval;
  1646. status = LDAP_OPERATIONS_ERROR;
  1647. }
  1648. cleanup:
  1649. slapi_ch_free_string(&filter);
  1650. slapi_ch_free_string(&strval);
  1651. slapi_free_search_results_internal(pb);
  1652. slapi_pblock_destroy(pb);
  1653. return status;
  1654. }
  1655. /*
  1656. * Perform ldap operationally atomic increment
  1657. * Return the next value to be assigned
  1658. */
  1659. static int dna_get_next_value(struct configEntry *config_entry,
  1660. char **next_value_ret)
  1661. {
  1662. Slapi_PBlock *pb = NULL;
  1663. LDAPMod mod_replace;
  1664. LDAPMod *mods[2];
  1665. char *replace_val[2];
  1666. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1667. char next_value[17];
  1668. PRUint64 setval = 0;
  1669. PRUint64 nextval = 0;
  1670. int ret;
  1671. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1672. "--> dna_get_next_value\n");
  1673. /* get the lock to prevent contention with other threads over
  1674. * the next new value for this range. */
  1675. slapi_lock_mutex(config_entry->lock);
  1676. /* get the first value */
  1677. ret = dna_first_free_value(config_entry, &setval);
  1678. if (LDAP_SUCCESS != ret) {
  1679. /* check if we overflowed the configured range */
  1680. if (setval > config_entry->maxval) {
  1681. /* try for a new range or fail */
  1682. ret = dna_fix_maxval(config_entry);
  1683. if (LDAP_SUCCESS != ret) {
  1684. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1685. "dna_get_next_value: no more values available!!\n");
  1686. goto done;
  1687. }
  1688. /* get the first value from our newly extended range */
  1689. ret = dna_first_free_value(config_entry, &setval);
  1690. if (LDAP_SUCCESS != ret)
  1691. goto done;
  1692. } else {
  1693. /* dna_first_free_value() failed for some unknown reason */
  1694. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1695. "dna_get_next_value: failed to allocate a new ID!!\n");
  1696. goto done;
  1697. }
  1698. }
  1699. nextval = setval + config_entry->interval;
  1700. /* update nextval if we have not reached the end
  1701. * of our current range */
  1702. if ((config_entry->maxval == -1) ||
  1703. (nextval <= (config_entry->maxval + config_entry->interval))) {
  1704. /* try to set the new next value in the config entry */
  1705. PR_snprintf(next_value, sizeof(next_value),"%" NSPRIu64, nextval);
  1706. /* set up our replace modify operation */
  1707. replace_val[0] = next_value;
  1708. replace_val[1] = 0;
  1709. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1710. mod_replace.mod_type = DNA_NEXTVAL;
  1711. mod_replace.mod_values = replace_val;
  1712. mods[0] = &mod_replace;
  1713. mods[1] = 0;
  1714. pb = slapi_pblock_new();
  1715. if (NULL == pb) {
  1716. ret = LDAP_OPERATIONS_ERROR;
  1717. goto done;
  1718. }
  1719. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1720. mods, 0, 0, getPluginID(), 0);
  1721. slapi_modify_internal_pb(pb);
  1722. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1723. }
  1724. if (LDAP_SUCCESS == ret) {
  1725. slapi_ch_free_string(next_value_ret);
  1726. *next_value_ret = slapi_ch_smprintf("%" NSPRIu64, setval);
  1727. if (NULL == *next_value_ret) {
  1728. ret = LDAP_OPERATIONS_ERROR;
  1729. goto done;
  1730. }
  1731. /* update our cached config */
  1732. dna_notice_allocation(config_entry, nextval, setval, 1);
  1733. }
  1734. done:
  1735. slapi_unlock_mutex(config_entry->lock);
  1736. if (pb) {
  1737. slapi_pblock_destroy(pb);
  1738. }
  1739. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1740. "<-- dna_get_next_value\n");
  1741. return ret;
  1742. }
  1743. /*
  1744. * dna_update_shared_config()
  1745. *
  1746. * Updates the shared config entry if one is
  1747. * configured. Returns the LDAP result code.
  1748. *
  1749. * The lock for configEntry should be obtained
  1750. * before calling this function.
  1751. * */
  1752. static int
  1753. dna_update_shared_config(struct configEntry * config_entry)
  1754. {
  1755. int ret = LDAP_SUCCESS;
  1756. if (config_entry && config_entry->shared_cfg_dn) {
  1757. /* Update the shared config entry if one is configured */
  1758. Slapi_PBlock *pb = NULL;
  1759. LDAPMod mod_replace;
  1760. LDAPMod *mods[2];
  1761. char *replace_val[2];
  1762. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1763. char remaining_vals[17];
  1764. /* We store the number of remaining assigned values
  1765. * in the shared config entry. */
  1766. PR_snprintf(remaining_vals, sizeof(remaining_vals),"%" NSPRIu64, config_entry->remaining);
  1767. /* set up our replace modify operation */
  1768. replace_val[0] = remaining_vals;
  1769. replace_val[1] = 0;
  1770. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1771. mod_replace.mod_type = DNA_REMAINING;
  1772. mod_replace.mod_values = replace_val;
  1773. mods[0] = &mod_replace;
  1774. mods[1] = 0;
  1775. pb = slapi_pblock_new();
  1776. if (NULL == pb) {
  1777. ret = LDAP_OPERATIONS_ERROR;
  1778. } else {
  1779. slapi_modify_internal_set_pb(pb, config_entry->shared_cfg_dn,
  1780. mods, NULL, NULL, getPluginID(), 0);
  1781. slapi_modify_internal_pb(pb);
  1782. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1783. /* If the shared config for this instance doesn't
  1784. * already exist, we add it. */
  1785. if (ret == LDAP_NO_SUCH_OBJECT) {
  1786. Slapi_Entry *e = NULL;
  1787. /* Set up the new shared config entry */
  1788. e = slapi_entry_alloc();
  1789. /* the entry now owns the dup'd dn */
  1790. slapi_entry_init(e, slapi_ch_strdup(config_entry->shared_cfg_dn), NULL);
  1791. slapi_entry_add_string(e, SLAPI_ATTR_OBJECTCLASS, "extensibleObject");
  1792. slapi_entry_add_string(e, DNA_HOSTNAME, hostname);
  1793. slapi_entry_add_string(e, DNA_PORTNUM, portnum);
  1794. if (secureportnum) {
  1795. slapi_entry_add_string(e, DNA_SECURE_PORTNUM, secureportnum);
  1796. }
  1797. slapi_entry_add_string(e, DNA_REMAINING, remaining_vals);
  1798. /* clear pb for re-use */
  1799. slapi_pblock_init(pb);
  1800. /* e will be consumed by slapi_add_internal() */
  1801. slapi_add_entry_internal_set_pb(pb, e, NULL, getPluginID(), 0);
  1802. slapi_add_internal_pb(pb);
  1803. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1804. }
  1805. if (ret != LDAP_SUCCESS) {
  1806. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1807. "dna_update_shared_config: Unable to update shared config entry: %s [error %d]\n",
  1808. config_entry->shared_cfg_dn, ret);
  1809. }
  1810. slapi_pblock_destroy(pb);
  1811. pb = NULL;
  1812. }
  1813. }
  1814. return ret;
  1815. }
  1816. /*
  1817. * dna_update_next_range()
  1818. *
  1819. * Sets the proper value for the next range in
  1820. * all configuration entries and in-memory cache.
  1821. *
  1822. * The range you are updating should be locked
  1823. * before calling this function.
  1824. */
  1825. static int
  1826. dna_update_next_range(struct configEntry *config_entry,
  1827. PRUint64 lower, PRUint64 upper)
  1828. {
  1829. Slapi_PBlock *pb = NULL;
  1830. LDAPMod mod_replace;
  1831. LDAPMod *mods[2];
  1832. char *replace_val[2];
  1833. /* 32 for the two numbers, 1 for the '-', and one for the '\0' */
  1834. char nextrange_value[34];
  1835. int ret = 0;
  1836. /* Try to set the new next range in the config entry. */
  1837. PR_snprintf(nextrange_value, sizeof(nextrange_value), "%" NSPRIu64 "-%" NSPRIu64,
  1838. lower, upper);
  1839. /* set up our replace modify operation */
  1840. replace_val[0] = nextrange_value;
  1841. replace_val[1] = 0;
  1842. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1843. mod_replace.mod_type = DNA_NEXT_RANGE;
  1844. mod_replace.mod_values = replace_val;
  1845. mods[0] = &mod_replace;
  1846. mods[1] = 0;
  1847. pb = slapi_pblock_new();
  1848. if (NULL == pb) {
  1849. ret = LDAP_OPERATIONS_ERROR;
  1850. goto bail;
  1851. }
  1852. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1853. mods, 0, 0, getPluginID(), 0);
  1854. slapi_modify_internal_pb(pb);
  1855. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1856. slapi_pblock_destroy(pb);
  1857. pb = NULL;
  1858. if (ret != LDAP_SUCCESS) {
  1859. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1860. "dna_update_next_range: Error updating "
  1861. "configuration entry [err=%d]\n", ret);
  1862. } else {
  1863. /* update the cached config and the shared config */
  1864. config_entry->next_range_lower = lower;
  1865. config_entry->next_range_upper = upper;
  1866. dna_notice_allocation(config_entry, 0, 0, 0);
  1867. }
  1868. bail:
  1869. return ret;
  1870. }
  1871. /* dna_activate_next_range()
  1872. *
  1873. * Makes the next range the active range. This
  1874. * will update the config entry, the in-memory
  1875. * config info, and the shared config entry.
  1876. *
  1877. * The lock for configEntry should
  1878. * be obtained before calling this function.
  1879. */
  1880. static int
  1881. dna_activate_next_range(struct configEntry *config_entry)
  1882. {
  1883. Slapi_PBlock *pb = NULL;
  1884. LDAPMod mod_maxval;
  1885. LDAPMod mod_nextval;
  1886. LDAPMod mod_nextrange;
  1887. LDAPMod *mods[4];
  1888. char *maxval_vals[2];
  1889. char *nextval_vals[2];
  1890. char *nextrange_vals[1];
  1891. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1892. char maxval_val[17];
  1893. char nextval_val[17];
  1894. int ret = 0;
  1895. /* Setup the modify operation for the config entry */
  1896. PR_snprintf(maxval_val, sizeof(maxval_val),"%" NSPRIu64, config_entry->next_range_upper);
  1897. PR_snprintf(nextval_val, sizeof(nextval_val),"%" NSPRIu64, config_entry->next_range_lower);
  1898. maxval_vals[0] = maxval_val;
  1899. maxval_vals[1] = 0;
  1900. nextval_vals[0] = nextval_val;
  1901. nextval_vals[1] = 0;
  1902. nextrange_vals[0] = 0;
  1903. mod_maxval.mod_op = LDAP_MOD_REPLACE;
  1904. mod_maxval.mod_type = DNA_MAXVAL;
  1905. mod_maxval.mod_values = maxval_vals;
  1906. mod_nextval.mod_op = LDAP_MOD_REPLACE;
  1907. mod_nextval.mod_type = DNA_NEXTVAL;
  1908. mod_nextval.mod_values = nextval_vals;
  1909. mod_nextrange.mod_op = LDAP_MOD_DELETE;
  1910. mod_nextrange.mod_type = DNA_NEXT_RANGE;
  1911. mod_nextrange.mod_values = nextrange_vals;
  1912. mods[0] = &mod_maxval;
  1913. mods[1] = &mod_nextval;
  1914. mods[2] = &mod_nextrange;
  1915. mods[3] = 0;
  1916. /* Update the config entry first */
  1917. pb = slapi_pblock_new();
  1918. if (NULL == pb) {
  1919. ret = LDAP_OPERATIONS_ERROR;
  1920. goto bail;
  1921. }
  1922. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1923. mods, 0, 0, getPluginID(), 0);
  1924. slapi_modify_internal_pb(pb);
  1925. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1926. slapi_pblock_destroy(pb);
  1927. pb = NULL;
  1928. if (ret != LDAP_SUCCESS) {
  1929. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1930. "dna_activate_next_range: Error updating "
  1931. "configuration entry [err=%d]\n", ret);
  1932. } else {
  1933. /* Update the in-memory config info */
  1934. config_entry->maxval = config_entry->next_range_upper;
  1935. config_entry->nextval = config_entry->next_range_lower;
  1936. config_entry->next_range_upper = 0;
  1937. config_entry->next_range_lower = 0;
  1938. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1939. config_entry->interval);
  1940. /* update the shared configuration */
  1941. dna_update_shared_config(config_entry);
  1942. }
  1943. bail:
  1944. return ret;
  1945. }
  1946. /*
  1947. * dna_is_replica_bind_dn()
  1948. *
  1949. * Checks if the passed in DN is the replica bind DN. This
  1950. * is used to check if a user is allowed to request range
  1951. * from us.
  1952. *
  1953. * Returns 1 if bind_dn matches the replica bind dn, 0 otherwise. */
  1954. static int dna_is_replica_bind_dn(char *range_dn, char *bind_dn)
  1955. {
  1956. char *replica_dn = NULL;
  1957. Slapi_DN *replica_sdn = NULL;
  1958. char *replica_bind_dn = NULL;
  1959. Slapi_DN *replica_bind_sdn = NULL;
  1960. Slapi_DN *range_sdn = NULL;
  1961. Slapi_DN *bind_sdn = NULL;
  1962. Slapi_Entry *e = NULL;
  1963. char *attrs[2];
  1964. Slapi_Backend *be = NULL;
  1965. const char *be_suffix = NULL;
  1966. int ret = 0;
  1967. /* Find the backend suffix where the shared config is stored. */
  1968. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  1969. if ((be = slapi_be_select(range_sdn)) != NULL) {
  1970. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  1971. }
  1972. /* Fetch the "cn=replica" entry for the backend that stores
  1973. * the shared config. We need to see what the configured
  1974. * replica bind DN is. */
  1975. if (be_suffix) {
  1976. /* This function converts the old DN style to the new one. */
  1977. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  1978. if (NULL == replica_dn) {
  1979. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1980. "dna_is_replica_bind_dn: failed to create "
  1981. "replica dn for %s\n", be_suffix);
  1982. return 1;
  1983. }
  1984. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  1985. attrs[0] = DNA_REPL_BIND_DN;
  1986. attrs[1] = 0;
  1987. /* Find cn=replica entry via search */
  1988. slapi_search_internal_get_entry(replica_sdn, attrs, &e, getPluginID());
  1989. if (e) {
  1990. replica_bind_dn = slapi_entry_attr_get_charptr(e, DNA_REPL_BIND_DN);
  1991. } else {
  1992. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1993. "dna_is_replica_bind_dn: Failed to fetch replica entry "
  1994. "for range %s\n", range_dn);
  1995. }
  1996. }
  1997. if (replica_bind_dn) {
  1998. /* Compare the passed in bind dn to the replica bind dn */
  1999. bind_sdn = slapi_sdn_new_dn_byref(bind_dn);
  2000. replica_bind_sdn = slapi_sdn_new_dn_passin(replica_bind_dn);
  2001. if (slapi_sdn_compare(bind_sdn, replica_bind_sdn) == 0) {
  2002. ret = 1;
  2003. }
  2004. }
  2005. slapi_entry_free(e);
  2006. slapi_sdn_free(&range_sdn);
  2007. slapi_sdn_free(&replica_sdn);
  2008. slapi_sdn_free(&replica_bind_sdn);
  2009. slapi_sdn_free(&bind_sdn);
  2010. return ret;
  2011. }
  2012. static int dna_get_replica_bind_creds(char *range_dn, struct dnaServer *server,
  2013. char **bind_dn, char **bind_passwd,
  2014. char **bind_method, int *is_ssl, int *port)
  2015. {
  2016. Slapi_PBlock *pb = NULL;
  2017. Slapi_DN *range_sdn = NULL;
  2018. char *replica_dn = NULL;
  2019. Slapi_Backend *be = NULL;
  2020. const char *be_suffix = NULL;
  2021. char *attrs[6];
  2022. char *filter = NULL;
  2023. char *bind_cred = NULL;
  2024. char *transport = NULL;
  2025. Slapi_Entry **entries = NULL;
  2026. int ret = LDAP_OPERATIONS_ERROR;
  2027. /* Find the backend suffix where the shared config is stored. */
  2028. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2029. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2030. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2031. }
  2032. /* Fetch the replication agreement entry */
  2033. if (be_suffix) {
  2034. /* This function converts the old DN style to the new one. */
  2035. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2036. if (NULL == replica_dn) {
  2037. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2038. "dna_get_replica_bind_creds: failed to create "
  2039. "replica dn for %s\n", be_suffix);
  2040. ret = LDAP_PARAM_ERROR;
  2041. goto bail;
  2042. }
  2043. filter = slapi_ch_smprintf("(&(nsds5ReplicaHost=%s)(|(" DNA_REPL_PORT "=%u)"
  2044. "(" DNA_REPL_PORT "=%u)))",
  2045. server->host, server->port, server->secureport);
  2046. attrs[0] = DNA_REPL_BIND_DN;
  2047. attrs[1] = DNA_REPL_CREDS;
  2048. attrs[2] = DNA_REPL_BIND_METHOD;
  2049. attrs[3] = DNA_REPL_TRANSPORT;
  2050. attrs[4] = DNA_REPL_PORT;
  2051. attrs[5] = 0;
  2052. pb = slapi_pblock_new();
  2053. if (NULL == pb) {
  2054. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2055. "dna_get_replica_bind_creds: Failed to "
  2056. "allocate pblock\n");
  2057. goto bail;
  2058. }
  2059. slapi_search_internal_set_pb(pb, replica_dn,
  2060. LDAP_SCOPE_ONELEVEL, filter,
  2061. attrs, 0, NULL, NULL, getPluginID(), 0);
  2062. slapi_search_internal_pb(pb);
  2063. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2064. if (LDAP_SUCCESS != ret) {
  2065. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2066. "dna_get_replica_bind_creds: Failed to fetch replica "
  2067. "bind credentials for range %s, server %s, port %u [error %d]\n",
  2068. range_dn, server->host, server->port, ret);
  2069. goto bail;
  2070. }
  2071. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2072. &entries);
  2073. if (NULL == entries || NULL == entries[0]) {
  2074. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2075. "dna_get_replica_bind_creds: Failed to fetch replication "
  2076. "agreement for range %s, server %s, port %u\n", range_dn,
  2077. server->host, server->port);
  2078. ret = LDAP_OPERATIONS_ERROR;
  2079. goto bail;
  2080. }
  2081. /* Get the replication bind dn and password from the agreement. It
  2082. * is up to the caller to free these when they are finished. */
  2083. slapi_ch_free_string(bind_dn);
  2084. slapi_ch_free_string(bind_method);
  2085. *bind_dn = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_DN);
  2086. *bind_method = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_METHOD);
  2087. bind_cred = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_CREDS);
  2088. transport = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_TRANSPORT);
  2089. *port = slapi_entry_attr_get_int(entries[0], DNA_REPL_PORT);
  2090. /* Check if we should use SSL */
  2091. if (transport && (strcasecmp(transport, "SSL") == 0)) {
  2092. *is_ssl = 1;
  2093. } else if (transport && (strcasecmp(transport, "TLS") == 0)) {
  2094. *is_ssl = 2;
  2095. } else {
  2096. *is_ssl = 0;
  2097. }
  2098. /* fix up the bind method */
  2099. if ((NULL == *bind_method) || (strcasecmp(*bind_method, "SIMPLE") == 0)) {
  2100. slapi_ch_free_string(bind_method);
  2101. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2102. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2103. slapi_ch_free_string(bind_method);
  2104. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2105. } else if (strcasecmp(*bind_method, "SASL/GSSAPI") == 0) {
  2106. slapi_ch_free_string(bind_method);
  2107. *bind_method = slapi_ch_strdup("GSSAPI");
  2108. } else if (strcasecmp(*bind_method, "SASL/DIGEST-MD5") == 0) {
  2109. slapi_ch_free_string(bind_method);
  2110. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2111. } else { /* some other weird value */
  2112. ; /* just use it directly */
  2113. }
  2114. /* Decode the password */
  2115. if (bind_cred) {
  2116. int pw_ret = 0;
  2117. slapi_ch_free_string(bind_passwd);
  2118. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2119. if (pw_ret == -1) {
  2120. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2121. "dna_get_replica_bind_creds: Failed to decode "
  2122. "replica bind credentials for range %s, server %s, "
  2123. "port %u\n", range_dn, server->host, server->port);
  2124. goto bail;
  2125. } else if (pw_ret != 0) {
  2126. /* The password was already in clear text, so pw_rever_decode
  2127. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2128. * to prevent a double free. The memory is now owned by
  2129. * bind_passwd, which is the callers responsibility to free. */
  2130. bind_cred = NULL;
  2131. }
  2132. }
  2133. }
  2134. /* If we didn't get both a bind DN and a decoded password,
  2135. * then just free everything and return an error. */
  2136. if (*bind_dn && *bind_passwd) {
  2137. ret = 0;
  2138. } else {
  2139. slapi_ch_free_string(bind_dn);
  2140. slapi_ch_free_string(bind_passwd);
  2141. slapi_ch_free_string(bind_method);
  2142. }
  2143. bail:
  2144. slapi_ch_free_string(&filter);
  2145. slapi_sdn_free(&range_sdn);
  2146. slapi_ch_free_string(&replica_dn);
  2147. slapi_ch_free_string(&bind_cred);
  2148. slapi_free_search_results_internal(pb);
  2149. slapi_pblock_destroy(pb);
  2150. return ret;
  2151. }
  2152. /*
  2153. * dna_list_contains_type()
  2154. *
  2155. * Checks if a type is contained in a list of types.
  2156. * Returns 1 if the type is found, 0 otherwise.
  2157. */
  2158. static int
  2159. dna_list_contains_type(char **list, char *type)
  2160. {
  2161. int ret = 0;
  2162. int i = 0;
  2163. if (list && type) {
  2164. for (i = 0; list[i]; i++) {
  2165. if (slapi_attr_types_equivalent(type, list[i])) {
  2166. ret = 1;
  2167. break;
  2168. }
  2169. }
  2170. }
  2171. return ret;
  2172. }
  2173. /*
  2174. * dna_list_contains_types()
  2175. *
  2176. * Checks if all types in one list (types) are contained
  2177. * in another list of types (list). Returns 1 if all
  2178. * types are found, 0 otherwise.
  2179. */
  2180. static int
  2181. dna_list_contains_types(char **list, char **types)
  2182. {
  2183. int ret = 1;
  2184. int i = 0;
  2185. int j = 0;
  2186. if (list && types) {
  2187. for (i = 0; types[i]; i++) {
  2188. int found = 0;
  2189. for (j = 0; list[j]; j++) {
  2190. if (slapi_attr_types_equivalent(types[i], list[i])) {
  2191. found = 1;
  2192. break;
  2193. }
  2194. }
  2195. if (!found) {
  2196. ret = 0;
  2197. break;
  2198. }
  2199. }
  2200. } else {
  2201. ret = 0;
  2202. }
  2203. return ret;
  2204. }
  2205. /*
  2206. * dna_list_remove_type()
  2207. *
  2208. * Removes a type from a list of types.
  2209. */
  2210. static void
  2211. dna_list_remove_type(char **list, char *type)
  2212. {
  2213. int i = 0;
  2214. int found_type = 0;
  2215. if (list && type) {
  2216. /* Go through the list until we find the type that
  2217. * we want to remove. We simply free the type we
  2218. * want to remove and shift the remaining array
  2219. * elements down by one index. This will leave us
  2220. * with two NULL elements at the end of the list,
  2221. * but this should not cause any problems. */
  2222. for (i = 0; list[i]; i++) {
  2223. if (found_type) {
  2224. list[i] = list[i + 1];
  2225. } else if (slapi_attr_types_equivalent(type, list[i])) {
  2226. slapi_ch_free_string(&list[i]);
  2227. list[i] = list[i + 1];
  2228. found_type = 1;
  2229. }
  2230. }
  2231. }
  2232. }
  2233. /*
  2234. * dna_is_multitype_range()
  2235. *
  2236. * Returns 1 if the range has multiple types configured.
  2237. * Returns 0 otherwise.
  2238. */
  2239. static int dna_is_multitype_range(struct configEntry *config_entry)
  2240. {
  2241. int ret = 0;
  2242. if (config_entry && config_entry->types && config_entry->types[1]) {
  2243. ret = 1;
  2244. }
  2245. return ret;
  2246. }
  2247. /*
  2248. * dna_create_valcheck_filter()
  2249. *
  2250. * Creates a filter string used to check if a value is free. If
  2251. * filter already holds a valid pointer, it is assumed to have enough
  2252. * space to hold the filter. This allows the same memory to be used
  2253. * over and over when you only want to change the assertion value.
  2254. * If filter contains a NULL pointer, a new string of the appropriate
  2255. * size will be allocated. The caller must free this when finished.
  2256. */
  2257. static void
  2258. dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter)
  2259. {
  2260. int filterlen = 0;
  2261. int typeslen = 0;
  2262. int i = 0;
  2263. int bytes_out = 0;
  2264. int multitype = 0;
  2265. /* Just return if we didn't get an address for the filter. */
  2266. if (filter == NULL) {
  2267. return;
  2268. }
  2269. /* To determine the filter length, we add together the following:
  2270. *
  2271. * - the string length of the filter in the config
  2272. * - the string length sum of all configured types
  2273. * - 23 bytes for each type (20 for the max string
  2274. * representation of a NSPRIu64, 3 for "(=)"
  2275. * - 3 bytes for the beginning and end of the filter - "(&" and ")"
  2276. * - 3 bytes to OR together multiple types (if present) - "(|" and ")"
  2277. * - the string length of the prefix (if one is configured) for each type
  2278. * - 1 byte for the trailing \0
  2279. *
  2280. * The filter length should be the same every time if the config struct
  2281. * has not been changed. We need to calculate the filter length even
  2282. * if we are reusing memory since we have no other way of knowing the
  2283. * length used when it was originally allocated. We trust the caller
  2284. * to only reuse the pointer with the same config struct.
  2285. */
  2286. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2287. typeslen += strlen(config_entry->types[i]);
  2288. }
  2289. if (i > 1) {
  2290. multitype = 1;
  2291. }
  2292. filterlen = strlen(config_entry->filter) + typeslen +
  2293. (i * 23) + 3 + 1 +
  2294. (config_entry->prefix ? (i * strlen(config_entry->prefix)) : 0) +
  2295. (multitype ? 3 : 0);
  2296. /* Allocate space for the filter if it hasn't been allocated yet. */
  2297. if (*filter == NULL) {
  2298. *filter = slapi_ch_malloc(filterlen);
  2299. }
  2300. /* Write out the beginning of the filter. If multiple types
  2301. * are configured, we need to OR together the search clauses
  2302. * for the types. */
  2303. if (multitype) {
  2304. bytes_out = snprintf(*filter, filterlen, "(&%s(|", config_entry->filter);
  2305. } else {
  2306. bytes_out = snprintf(*filter, filterlen, "(&%s", config_entry->filter);
  2307. }
  2308. /* Loop through the types and append each filter clause. */
  2309. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2310. bytes_out += snprintf(*filter + bytes_out, filterlen - bytes_out,
  2311. "(%s=%s%" PRIu64 ")", config_entry->types[i],
  2312. config_entry->prefix ? config_entry->prefix : "",
  2313. value);
  2314. }
  2315. /* Append the end of the filter. We need an extra paren
  2316. * to close out the OR if we have multiple types. */
  2317. if (multitype) {
  2318. strncat(*filter, "))", filterlen - bytes_out);
  2319. } else {
  2320. strncat(*filter, ")", filterlen - bytes_out);
  2321. }
  2322. }
  2323. /* for mods and adds:
  2324. where dn's are supplied, the closest in scope
  2325. is used as long as the type filter matches
  2326. and the type has not been generated yet.
  2327. */
  2328. static int dna_pre_op(Slapi_PBlock * pb, int modtype)
  2329. {
  2330. char *dn = 0;
  2331. PRCList *list = 0;
  2332. struct configEntry *config_entry = 0;
  2333. struct slapi_entry *e = 0;
  2334. Slapi_Entry *resulting_e = 0;
  2335. char *value = 0;
  2336. char **types_to_generate = NULL;
  2337. char **generated_types = NULL;
  2338. Slapi_Mods *smods = 0;
  2339. Slapi_Mod *smod = 0;
  2340. LDAPMod **mods;
  2341. int free_entry = 0;
  2342. char *errstr = NULL;
  2343. int i = 0;
  2344. int ret = 0;
  2345. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2346. "--> dna_pre_op\n");
  2347. /* Just bail if we aren't ready to service requests yet. */
  2348. if (!g_plugin_started)
  2349. goto bail;
  2350. if (0 == (dn = dna_get_dn(pb)))
  2351. goto bail;
  2352. if (LDAP_CHANGETYPE_ADD == modtype) {
  2353. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  2354. } else {
  2355. /* xxxPAR: Ideally SLAPI_MODIFY_EXISTING_ENTRY should be
  2356. * available but it turns out that is only true if you are
  2357. * a dbm backend pre-op plugin - lucky dbm backend pre-op
  2358. * plugins.
  2359. * I think that is wrong since the entry is useful for filter
  2360. * tests and schema checks and this plugin shouldn't be limited
  2361. * to a single backend type, but I don't want that fight right
  2362. * now so we go get the entry here
  2363. *
  2364. slapi_pblock_get( pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  2365. */
  2366. Slapi_DN *tmp_dn = slapi_sdn_new_dn_byref(dn);
  2367. if (tmp_dn) {
  2368. slapi_search_internal_get_entry(tmp_dn, 0, &e, getPluginID());
  2369. slapi_sdn_free(&tmp_dn);
  2370. free_entry = 1;
  2371. }
  2372. /* grab the mods - we'll put them back later with
  2373. * our modifications appended
  2374. */
  2375. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  2376. smods = slapi_mods_new();
  2377. slapi_mods_init_passin(smods, mods);
  2378. /* We need the resulting entry after the mods are applied to
  2379. * see if the entry is within the scope. */
  2380. if (e) {
  2381. resulting_e = slapi_entry_dup(e);
  2382. if (mods && (slapi_entry_apply_mods(resulting_e, mods) != LDAP_SUCCESS)) {
  2383. /* The mods don't apply cleanly, so we just let this op go
  2384. * to let the main server handle it. */
  2385. goto bailmod;
  2386. }
  2387. }
  2388. }
  2389. if (0 == e)
  2390. goto bailmod;
  2391. if (dna_dn_is_config(dn)) {
  2392. /* Validate config changes, but don't apply them.
  2393. * This allows us to reject invalid config changes
  2394. * here at the pre-op stage. Applying the config
  2395. * needs to be done at the post-op stage. */
  2396. Slapi_Entry *test_e = NULL;
  2397. /* For a MOD, we need to check the resulting entry */
  2398. if (LDAP_CHANGETYPE_ADD == modtype) {
  2399. test_e = e;
  2400. } else {
  2401. test_e = resulting_e;
  2402. }
  2403. if (dna_parse_config_entry(test_e, 0) != DNA_SUCCESS) {
  2404. /* Refuse the operation if config parsing failed. */
  2405. ret = LDAP_UNWILLING_TO_PERFORM;
  2406. if (LDAP_CHANGETYPE_ADD == modtype) {
  2407. errstr = slapi_ch_smprintf("Not a valid DNA configuration entry.");
  2408. } else {
  2409. errstr = slapi_ch_smprintf("Changes result in an invalid "
  2410. "DNA configuration.");
  2411. }
  2412. }
  2413. /* We're done, so just bail. */
  2414. goto bailmod;
  2415. }
  2416. dna_read_lock();
  2417. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2418. list = PR_LIST_HEAD(dna_global_config);
  2419. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  2420. config_entry = (struct configEntry *) list;
  2421. /* Did we already service all of these configured types? */
  2422. if (dna_list_contains_types(generated_types, config_entry->types)) {
  2423. goto next;
  2424. }
  2425. /* is the entry in scope? */
  2426. if (config_entry->scope) {
  2427. if (!slapi_dn_issuffix(dn, config_entry->scope))
  2428. goto next;
  2429. }
  2430. /* does the entry match the filter? */
  2431. if (config_entry->slapi_filter) {
  2432. Slapi_Entry *test_e = NULL;
  2433. /* For a MOD operation, we need to check the filter
  2434. * against the resulting entry. */
  2435. if (LDAP_CHANGETYPE_ADD == modtype) {
  2436. test_e = e;
  2437. } else {
  2438. test_e = resulting_e;
  2439. }
  2440. if (LDAP_SUCCESS != slapi_vattr_filter_test(pb,
  2441. test_e,
  2442. config_entry->
  2443. slapi_filter, 0))
  2444. goto next;
  2445. }
  2446. if (LDAP_CHANGETYPE_ADD == modtype) {
  2447. if (config_entry->types && dna_is_multitype_range(config_entry)) {
  2448. /* For a multi-type range, we only generate a value
  2449. * for types where the magic value is set. We do not
  2450. * generate a value for missing types. */
  2451. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2452. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  2453. if (value && !slapi_UTF8CASECMP(config_entry->generate, value)) {
  2454. slapi_ch_array_add(&types_to_generate,
  2455. slapi_ch_strdup(config_entry->types[i]));
  2456. }
  2457. slapi_ch_free_string(&value);
  2458. }
  2459. } else {
  2460. /* For a single type range, we generate the value if
  2461. * the magic value is set or if the type is missing. */
  2462. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  2463. if ((value && !slapi_UTF8CASECMP(config_entry->generate, value))
  2464. || 0 == value) {
  2465. slapi_ch_array_add(&types_to_generate,
  2466. slapi_ch_strdup(config_entry->types[0]));
  2467. }
  2468. slapi_ch_free_string(&value);
  2469. }
  2470. } else {
  2471. /* check mods for magic value */
  2472. Slapi_Mod *next_mod = slapi_mod_new();
  2473. smod = slapi_mods_get_first_smod(smods, next_mod);
  2474. while (smod) {
  2475. char *type = (char *)
  2476. slapi_mod_get_type(smod);
  2477. /* See if the type matches any configured type. */
  2478. if (dna_list_contains_type(config_entry->types, type)) {
  2479. /* If all values are being deleted, we need to
  2480. * generate a new value. We don't do this for
  2481. * multi-type ranges since they require the magic
  2482. * value to be specified to trigger generation. */
  2483. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  2484. !dna_is_multitype_range(config_entry)) {
  2485. int numvals = slapi_mod_get_num_values(smod);
  2486. if (numvals == 0) {
  2487. slapi_ch_array_add(&types_to_generate,
  2488. slapi_ch_strdup(type));
  2489. } else {
  2490. Slapi_Attr *attr = NULL;
  2491. int e_numvals = 0;
  2492. slapi_entry_attr_find(e, type, &attr);
  2493. if (attr) {
  2494. slapi_attr_get_numvalues(attr, &e_numvals);
  2495. if (numvals >= e_numvals) {
  2496. slapi_ch_array_add(&types_to_generate,
  2497. slapi_ch_strdup(type));
  2498. }
  2499. }
  2500. }
  2501. } else {
  2502. /* This is either adding or replacing a value */
  2503. struct berval *bv = slapi_mod_get_first_value(smod);
  2504. /* If this type is already in the to be generated
  2505. * list, a previous mod in this same modify operation
  2506. * either removed all values or set the magic value.
  2507. * It's possible that this mod is adding a valid value,
  2508. * which means we would not want to generate a new value.
  2509. * It is safe to remove this type from the to be
  2510. * generated list since it will be re-added here if
  2511. * necessary. */
  2512. if (dna_list_contains_type(types_to_generate, type)) {
  2513. dna_list_remove_type(types_to_generate, type);
  2514. }
  2515. /* If we have a value, see if it's the magic value. */
  2516. if (bv) {
  2517. int len = strlen(config_entry->generate);
  2518. if (len == bv->bv_len) {
  2519. if (!slapi_UTF8NCASECMP(bv->bv_val,
  2520. config_entry->generate,
  2521. len)) {
  2522. slapi_ch_array_add(&types_to_generate,
  2523. slapi_ch_strdup(type));
  2524. }
  2525. }
  2526. } else if (!dna_is_multitype_range(config_entry)) {
  2527. /* This is a replace with no new values, so we need
  2528. * to generate a new value if this is not a multi-type
  2529. * range. */
  2530. slapi_ch_array_add(&types_to_generate,
  2531. slapi_ch_strdup(type));
  2532. }
  2533. }
  2534. }
  2535. slapi_mod_done(next_mod);
  2536. smod = slapi_mods_get_next_smod(smods, next_mod);
  2537. }
  2538. slapi_mod_free(&next_mod);
  2539. }
  2540. /* We need to perform one last check for modify operations. If an
  2541. * entry within the scope has not triggered generation yet, we need
  2542. * to see if a value exists for the managed type in the resulting
  2543. * entry. This will catch a modify operation that brings an entry
  2544. * into scope for a managed range, but doesn't supply a value for
  2545. * the managed type. We don't do this for multi-type ranges. */
  2546. if ((LDAP_CHANGETYPE_MODIFY == modtype) && (!types_to_generate ||
  2547. (types_to_generate && !types_to_generate[0])) &&
  2548. !dna_is_multitype_range(config_entry)) {
  2549. Slapi_Attr *attr = NULL;
  2550. if (slapi_entry_attr_find(resulting_e, config_entry->types[0], &attr) != 0) {
  2551. slapi_ch_array_add(&types_to_generate,
  2552. slapi_ch_strdup(config_entry->types[0]));
  2553. }
  2554. }
  2555. if (types_to_generate && types_to_generate[0]) {
  2556. char *new_value;
  2557. int len;
  2558. /* create the value to add */
  2559. ret = dna_get_next_value(config_entry, &value);
  2560. if (DNA_SUCCESS != ret) {
  2561. errstr = slapi_ch_smprintf("Allocation of a new value for range"
  2562. " %s failed! Unable to proceed.",
  2563. config_entry->dn);
  2564. break;
  2565. }
  2566. len = strlen(value) + 1;
  2567. if (config_entry->prefix) {
  2568. len += strlen(config_entry->prefix);
  2569. }
  2570. new_value = slapi_ch_malloc(len);
  2571. if (config_entry->prefix) {
  2572. strcpy(new_value, config_entry->prefix);
  2573. strcat(new_value, value);
  2574. } else
  2575. strcpy(new_value, value);
  2576. /* do the mod */
  2577. if (LDAP_CHANGETYPE_ADD == modtype) {
  2578. /* add - add to entry */
  2579. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2580. slapi_entry_attr_set_charptr(e,
  2581. types_to_generate[i],
  2582. new_value);
  2583. }
  2584. } else {
  2585. /* mod - add to mods */
  2586. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2587. slapi_mods_add_string(smods,
  2588. LDAP_MOD_REPLACE,
  2589. types_to_generate[i], new_value);
  2590. }
  2591. }
  2592. /* Make sure we don't generate for this
  2593. * type again by keeping a list of types
  2594. * we have generated for already.
  2595. */
  2596. if (LDAP_SUCCESS == ret) {
  2597. if (generated_types == NULL) {
  2598. /* If we don't have a list of generated types yet,
  2599. * we can just use the types_to_generate list so
  2600. * we don't have to allocate anything. */
  2601. generated_types = types_to_generate;
  2602. types_to_generate = NULL;
  2603. } else {
  2604. /* Just reuse the elements out of types_to_generate for the
  2605. * generated types list to avoid allocating them again. */
  2606. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  2607. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  2608. types_to_generate[i] = NULL;
  2609. }
  2610. }
  2611. }
  2612. /* free up */
  2613. slapi_ch_free_string(&value);
  2614. slapi_ch_free_string(&new_value);
  2615. slapi_ch_array_free(types_to_generate);
  2616. }
  2617. next:
  2618. list = PR_NEXT_LINK(list);
  2619. }
  2620. }
  2621. dna_unlock();
  2622. bailmod:
  2623. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  2624. /* Put the updated mods back into place. */
  2625. mods = slapi_mods_get_ldapmods_passout(smods);
  2626. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  2627. slapi_mods_free(&smods);
  2628. }
  2629. bail:
  2630. slapi_ch_array_free(generated_types);
  2631. if (free_entry && e)
  2632. slapi_entry_free(e);
  2633. if (resulting_e)
  2634. slapi_entry_free(resulting_e);
  2635. if (ret) {
  2636. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2637. "dna_pre_op: operation failure [%d]\n", ret);
  2638. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  2639. slapi_ch_free((void **)&errstr);
  2640. ret = DNA_FAILURE;
  2641. }
  2642. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2643. "<-- dna_pre_op\n");
  2644. return ret;
  2645. }
  2646. static int dna_add_pre_op(Slapi_PBlock * pb)
  2647. {
  2648. return dna_pre_op(pb, LDAP_CHANGETYPE_ADD);
  2649. }
  2650. static int dna_mod_pre_op(Slapi_PBlock * pb)
  2651. {
  2652. return dna_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  2653. }
  2654. static int dna_config_check_post_op(Slapi_PBlock * pb)
  2655. {
  2656. char *dn;
  2657. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2658. "--> dna_config_check_post_op\n");
  2659. if ((dn = dna_get_dn(pb))) {
  2660. if (dna_dn_is_config(dn))
  2661. dna_load_plugin_config();
  2662. }
  2663. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2664. "<-- dna_config_check_post_op\n");
  2665. return 0;
  2666. }
  2667. /****************************************************
  2668. * Range Extension Extended Operation
  2669. ***************************************************/
  2670. static int dna_extend_exop(Slapi_PBlock *pb)
  2671. {
  2672. int ret = -1;
  2673. struct berval *reqdata = NULL;
  2674. BerElement *tmp_bere = NULL;
  2675. char *shared_dn = NULL;
  2676. char *bind_dn = NULL;
  2677. char *oid = NULL;
  2678. PRUint64 lower = 0;
  2679. PRUint64 upper = 0;
  2680. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2681. "--> dna_extend_exop\n");
  2682. /* Fetch the request OID */
  2683. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &oid);
  2684. if (!oid) {
  2685. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2686. "dna_extend_exop: Unable to retrieve request OID.\n");
  2687. goto free_and_return;
  2688. }
  2689. /* Make sure the request OID is correct. */
  2690. if (strcmp(oid, DNA_EXTEND_EXOP_REQUEST_OID) != 0) {
  2691. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2692. "dna_extend_exop: Received incorrect request OID.\n");
  2693. goto free_and_return;
  2694. }
  2695. /* Fetch the request data */
  2696. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &reqdata);
  2697. if (!reqdata) {
  2698. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2699. "dna_extend_exop: No request data received.\n");
  2700. goto free_and_return;
  2701. }
  2702. /* decode the exop */
  2703. if ((tmp_bere = ber_init(reqdata)) == NULL) {
  2704. ret = -1;
  2705. goto free_and_return;
  2706. }
  2707. if (ber_scanf(tmp_bere, "{a}", &shared_dn) == LBER_ERROR) {
  2708. ret = -1;
  2709. goto free_and_return;
  2710. }
  2711. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2712. "dna_extend_exop: received range extension "
  2713. "request for range [%s]\n", shared_dn);
  2714. /* Only allow range requests from the replication bind DN user */
  2715. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
  2716. if (!dna_is_replica_bind_dn(shared_dn, bind_dn)) {
  2717. ret = LDAP_INSUFFICIENT_ACCESS;
  2718. goto free_and_return;
  2719. }
  2720. /* See if we have the req. range configured.
  2721. * If so, we need to see if we have range to provide. */
  2722. ret = dna_release_range(shared_dn, &lower, &upper);
  2723. if (ret == LDAP_SUCCESS) {
  2724. /* We have range to give away, so construct
  2725. * and send the response. */
  2726. BerElement *respber = NULL;
  2727. struct berval *respdata = NULL;
  2728. struct berval range_low = {0, NULL};
  2729. struct berval range_high = {0, NULL};
  2730. char lowstr[16];
  2731. char highstr[16];
  2732. /* Create the exop response */
  2733. PR_snprintf(lowstr, sizeof(lowstr), "%" NSPRIu64, lower);
  2734. PR_snprintf(highstr, sizeof(highstr), "%" NSPRIu64, upper);
  2735. range_low.bv_val = lowstr;
  2736. range_low.bv_len = strlen(range_low.bv_val);
  2737. range_high.bv_val = highstr;
  2738. range_high.bv_len = strlen(range_high.bv_val);
  2739. if ((respber = ber_alloc()) == NULL) {
  2740. ret = LDAP_NO_MEMORY;
  2741. goto free_and_return;
  2742. }
  2743. if (LBER_ERROR == (ber_printf(respber, "{oo}",
  2744. range_low.bv_val, range_low.bv_len,
  2745. range_high.bv_val, range_high.bv_len))) {
  2746. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2747. "dna_extend_exop: Unable to encode exop response.\n");
  2748. ber_free(respber, 1);
  2749. ret = LDAP_ENCODING_ERROR;
  2750. goto free_and_return;
  2751. }
  2752. ber_flatten(respber, &respdata);
  2753. ber_free(respber, 1);
  2754. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, DNA_EXTEND_EXOP_RESPONSE_OID);
  2755. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, respdata);
  2756. /* send the response ourselves */
  2757. slapi_send_ldap_result( pb, ret, NULL, NULL, 0, NULL );
  2758. ret = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  2759. ber_bvfree(respdata);
  2760. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2761. "dna_extend_exop: Released range %" NSPRIu64 "-%" NSPRIu64 ".\n",
  2762. lower, upper);
  2763. }
  2764. free_and_return:
  2765. slapi_ch_free_string(&shared_dn);
  2766. slapi_ch_free_string(&bind_dn);
  2767. if (NULL != tmp_bere) {
  2768. ber_free(tmp_bere, 1);
  2769. tmp_bere = NULL;
  2770. }
  2771. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2772. "<-- dna_extend_exop\n");
  2773. return ret;
  2774. }
  2775. /*
  2776. * dna_release_range()
  2777. *
  2778. * Checks if we have any values that we can release
  2779. * for the range specified by range_dn.
  2780. */
  2781. static int
  2782. dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper)
  2783. {
  2784. int ret = 0;
  2785. int match = 0;
  2786. PRCList *list = NULL;
  2787. Slapi_DN *cfg_base_sdn = NULL;
  2788. Slapi_DN *range_sdn = NULL;
  2789. struct configEntry *config_entry = NULL;
  2790. int set_extend_flag = 0;
  2791. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2792. "--> dna_release_range\n");
  2793. if (range_dn) {
  2794. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2795. dna_read_lock();
  2796. /* Go through the config entries to see if we
  2797. * have a shared range configured that matches
  2798. * the range from the exop request. */
  2799. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2800. list = PR_LIST_HEAD(dna_global_config);
  2801. while ((list != dna_global_config) && match != 1) {
  2802. config_entry = (struct configEntry *)list;
  2803. cfg_base_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_base);
  2804. if (slapi_sdn_compare(cfg_base_sdn, range_sdn) == 0) {
  2805. /* We found a match. Set match flag to
  2806. * break out of the loop. */
  2807. match = 1;
  2808. } else {
  2809. config_entry = NULL;
  2810. list = PR_NEXT_LINK(list);
  2811. }
  2812. slapi_sdn_free(&cfg_base_sdn);
  2813. }
  2814. }
  2815. /* config_entry will point to our match if we found one */
  2816. if (config_entry) {
  2817. int release = 0;
  2818. Slapi_PBlock *pb = NULL;
  2819. LDAPMod mod_replace;
  2820. LDAPMod *mods[2];
  2821. char *replace_val[2];
  2822. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2823. char max_value[17];
  2824. /* Need to bail if we're performing a range request
  2825. * for this range. This is to prevent the case where two
  2826. * servers are asking each other for more range for the
  2827. * same managed range. This would result in a network
  2828. * deadlock until the idletimeout kills one of the
  2829. * connections. */
  2830. slapi_lock_mutex(config_entry->extend_lock);
  2831. if (config_entry->extend_in_progress) {
  2832. /* We're already processing a range extention, so bail. */
  2833. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2834. "dna_release_range: Already processing a "
  2835. "range extension request. Skipping request.\n");
  2836. slapi_unlock_mutex(config_entry->extend_lock);
  2837. ret = LDAP_UNWILLING_TO_PERFORM;
  2838. goto bail;
  2839. } else {
  2840. /* Set a flag indicating that we're attempting to extend this range */
  2841. config_entry->extend_in_progress = 1;
  2842. set_extend_flag = 1;
  2843. slapi_unlock_mutex(config_entry->extend_lock);
  2844. }
  2845. /* Obtain the lock for this range */
  2846. slapi_lock_mutex(config_entry->lock);
  2847. /* Refuse if we're at or below our threshold */
  2848. if (config_entry->remaining <= config_entry->threshold) {
  2849. ret = LDAP_UNWILLING_TO_PERFORM;
  2850. goto bail;
  2851. }
  2852. /* If we have a next range, we need to give up values from
  2853. * it instead of from the active range */
  2854. if (config_entry->next_range_lower != 0) {
  2855. /* Release up to half of our values from the next range. */
  2856. release = (((config_entry->next_range_upper - config_entry->next_range_lower + 1) /
  2857. 2) / config_entry->threshold) * config_entry->threshold;
  2858. if (release == 0) {
  2859. ret = LDAP_UNWILLING_TO_PERFORM;
  2860. goto bail;
  2861. }
  2862. *upper = config_entry->next_range_upper;
  2863. *lower = *upper - release + 1;
  2864. /* Try to set the new next range in the config */
  2865. ret = dna_update_next_range(config_entry, config_entry->next_range_lower,
  2866. *lower - 1);
  2867. } else {
  2868. /* We release up to half of our remaining values,
  2869. * but we'll only release a range that is a multiple
  2870. * of our threshold. */
  2871. release = ((config_entry->remaining / 2) /
  2872. config_entry->threshold) * config_entry->threshold;
  2873. if (release == 0) {
  2874. ret = LDAP_UNWILLING_TO_PERFORM;
  2875. goto bail;
  2876. }
  2877. /* We give away values from the upper end of our range. */
  2878. *upper = config_entry->maxval;
  2879. *lower = *upper - release + 1;
  2880. /* try to set the new maxval in the config entry */
  2881. PR_snprintf(max_value, sizeof(max_value),"%" NSPRIu64, (*lower - 1));
  2882. /* set up our replace modify operation */
  2883. replace_val[0] = max_value;
  2884. replace_val[1] = 0;
  2885. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2886. mod_replace.mod_type = DNA_MAXVAL;
  2887. mod_replace.mod_values = replace_val;
  2888. mods[0] = &mod_replace;
  2889. mods[1] = 0;
  2890. pb = slapi_pblock_new();
  2891. if (NULL == pb) {
  2892. ret = LDAP_OPERATIONS_ERROR;
  2893. goto bail;
  2894. }
  2895. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2896. mods, 0, 0, getPluginID(), 0);
  2897. slapi_modify_internal_pb(pb);
  2898. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2899. slapi_pblock_destroy(pb);
  2900. pb = NULL;
  2901. if (ret == LDAP_SUCCESS) {
  2902. /* Adjust maxval in our cached config and shared config */
  2903. config_entry->maxval = *lower - 1;
  2904. dna_notice_allocation(config_entry, config_entry->nextval, 0, 0);
  2905. }
  2906. }
  2907. if (ret != LDAP_SUCCESS) {
  2908. /* Updating the config failed, so reset. We don't
  2909. * want to give the caller any range */
  2910. *lower = 0;
  2911. *upper = 0;
  2912. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2913. "dna_release_range: Error updating "
  2914. "configuration entry [err=%d]\n", ret);
  2915. }
  2916. }
  2917. bail:
  2918. if (set_extend_flag) {
  2919. slapi_lock_mutex(config_entry->extend_lock);
  2920. config_entry->extend_in_progress = 0;
  2921. slapi_unlock_mutex(config_entry->extend_lock);
  2922. }
  2923. if (config_entry) {
  2924. slapi_unlock_mutex(config_entry->lock);
  2925. }
  2926. slapi_sdn_free(&range_sdn);
  2927. dna_unlock();
  2928. }
  2929. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2930. "<-- dna_release_range\n");
  2931. return ret;
  2932. }
  2933. /****************************************************
  2934. End of
  2935. Functions that actually do things other
  2936. than config and startup
  2937. ****************************************************/
  2938. /**
  2939. * debug functions to print config
  2940. */
  2941. void dna_dump_config()
  2942. {
  2943. PRCList *list;
  2944. dna_read_lock();
  2945. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2946. list = PR_LIST_HEAD(dna_global_config);
  2947. while (list != dna_global_config) {
  2948. dna_dump_config_entry((struct configEntry *) list);
  2949. list = PR_NEXT_LINK(list);
  2950. }
  2951. }
  2952. dna_unlock();
  2953. }
  2954. void dna_dump_config_entry(struct configEntry * entry)
  2955. {
  2956. int i = 0;
  2957. for (i = 0; entry->types && entry->types[i]; i++) {
  2958. printf("<---- type -----------> %s\n", entry->types[i]);
  2959. }
  2960. printf("<---- filter ---------> %s\n", entry->filter);
  2961. printf("<---- prefix ---------> %s\n", entry->prefix);
  2962. printf("<---- scope ----------> %s\n", entry->scope);
  2963. printf("<---- next value -----> %" PRIu64 "\n", entry->nextval);
  2964. printf("<---- max value ------> %" PRIu64 "\n", entry->maxval);
  2965. printf("<---- interval -------> %" PRIu64 "\n", entry->interval);
  2966. printf("<---- generate flag --> %s\n", entry->generate);
  2967. printf("<---- shared cfg base > %s\n", entry->shared_cfg_base);
  2968. printf("<---- shared cfg DN --> %s\n", entry->shared_cfg_dn);
  2969. printf("<---- threshold ------> %" PRIu64 "", entry->threshold);
  2970. }