dna.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484
  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. slapi_ch_free_string(&value);
  639. }
  640. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  641. "----------> %s [%s]\n", DNA_PREFIX, entry->prefix);
  642. /* Set the default interval to 1 */
  643. entry->interval = 1;
  644. #ifdef DNA_ENABLE_INTERVAL
  645. value = slapi_entry_attr_get_charptr(e, DNA_INTERVAL);
  646. if (value) {
  647. entry->interval = strtoull(value, 0, 0);
  648. slapi_ch_free_string(&value);
  649. }
  650. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  651. "----------> %s [%" NSPRIu64 "]\n", DNA_INTERVAL, entry->interval);
  652. #endif
  653. value = slapi_entry_attr_get_charptr(e, DNA_GENERATE);
  654. if (value) {
  655. entry->generate = value;
  656. }
  657. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  658. "----------> %s [%s]\n", DNA_GENERATE, entry->generate);
  659. value = slapi_entry_attr_get_charptr(e, DNA_FILTER);
  660. if (value) {
  661. entry->filter = value;
  662. if (NULL == (entry->slapi_filter = slapi_str2filter(value))) {
  663. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM ,
  664. "Error: Invalid search filter in entry [%s]: [%s]\n",
  665. entry->dn, value);
  666. ret = DNA_FAILURE;
  667. goto bail;
  668. }
  669. } else {
  670. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  671. "dna_parse_config_entry: The %s config "
  672. "setting is required for range %s.\n",
  673. DNA_FILTER, entry->dn);
  674. ret = DNA_FAILURE;
  675. goto bail;
  676. }
  677. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  678. "----------> %s [%s]\n", DNA_FILTER, value);
  679. value = slapi_entry_attr_get_charptr(e, DNA_SCOPE);
  680. if (value) {
  681. /* TODO - Allow multiple scope settings for a single range. This may
  682. * make ordering the scopes tough when we put them in the clist. */
  683. entry->scope = value;
  684. } else {
  685. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  686. "dna_parse_config_entry: The %s config "
  687. "config setting is required for range %s.\n",
  688. DNA_SCOPE, entry->dn);
  689. ret = DNA_FAILURE;
  690. goto bail;
  691. }
  692. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  693. "----------> %s [%s]\n", DNA_SCOPE, entry->scope);
  694. /* optional, if not specified set -1 which is converted to the max unisgnee
  695. * value */
  696. value = slapi_entry_attr_get_charptr(e, DNA_MAXVAL);
  697. if (value) {
  698. entry->maxval = strtoull(value, 0, 0);
  699. slapi_ch_free_string(&value);
  700. } else {
  701. entry->maxval = -1;
  702. }
  703. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  704. "----------> %s [%" NSPRIu64 "]\n", DNA_MAXVAL, entry->maxval);
  705. value = slapi_entry_attr_get_charptr(e, DNA_SHARED_CFG_DN);
  706. if (value) {
  707. Slapi_Entry *shared_e = NULL;
  708. Slapi_DN *sdn = NULL;
  709. char *normdn = NULL;
  710. sdn = slapi_sdn_new_dn_byref(value);
  711. if (sdn) {
  712. slapi_search_internal_get_entry(sdn, NULL, &shared_e, getPluginID());
  713. slapi_sdn_free(&sdn);
  714. }
  715. /* Make sure that the shared config entry exists. */
  716. if (!shared_e) {
  717. /* We didn't locate the shared config container entry. Log
  718. * a message and skip this config entry. */
  719. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  720. "dna_parse_config_entry: Unable to locate "
  721. "shared configuration entry (%s)\n", value);
  722. ret = DNA_FAILURE;
  723. slapi_ch_free_string(&value);
  724. goto bail;
  725. } else {
  726. slapi_entry_free(shared_e);
  727. shared_e = NULL;
  728. }
  729. normdn = slapi_create_dn_string("%s", value);
  730. if (NULL == normdn) {
  731. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  732. "dna_parse_config_entry: failed to normalize dn: "
  733. "%s\n", value);
  734. ret = DNA_FAILURE;
  735. goto bail;
  736. }
  737. entry->shared_cfg_base = normdn;
  738. /* We prepend the host & port of this instance as a
  739. * multi-part RDN for the shared config entry. */
  740. normdn = slapi_create_dn_string("%s=%s+%s=%s,%s", DNA_HOSTNAME,
  741. hostname, DNA_PORTNUM, portnum, normdn);
  742. if (NULL == normdn) {
  743. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  744. "dna_parse_config_entry: failed to create dn: "
  745. "%s=%s+%s=%s,%s", DNA_HOSTNAME,
  746. hostname, DNA_PORTNUM, portnum, value);
  747. ret = DNA_FAILURE;
  748. goto bail;
  749. }
  750. entry->shared_cfg_dn = normdn;
  751. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  752. "----------> %s [%s]\n", DNA_SHARED_CFG_DN,
  753. entry->shared_cfg_base);
  754. }
  755. value = slapi_entry_attr_get_charptr(e, DNA_THRESHOLD);
  756. if (value) {
  757. entry->threshold = strtoull(value, 0, 0);
  758. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  759. "----------> %s [%s]\n", DNA_THRESHOLD, value);
  760. slapi_ch_free_string(&value);
  761. } else {
  762. entry->threshold = 1;
  763. }
  764. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  765. "----------> %s [%" NSPRIu64 "]\n", DNA_THRESHOLD, entry->threshold);
  766. value = slapi_entry_attr_get_charptr(e, DNA_RANGE_REQUEST_TIMEOUT);
  767. if (value) {
  768. entry->timeout = strtoull(value, 0, 0);
  769. slapi_ch_free_string(&value);
  770. } else {
  771. entry->timeout = DNA_DEFAULT_TIMEOUT;
  772. }
  773. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  774. "----------> %s [%" NSPRIu64 "]\n", DNA_RANGE_REQUEST_TIMEOUT,
  775. entry->timeout);
  776. value = slapi_entry_attr_get_charptr(e, DNA_NEXT_RANGE);
  777. if (value) {
  778. char *p = NULL;
  779. /* the next range value is in the form "<lower>-<upper>" */
  780. if ((p = strstr(value, "-")) != NULL) {
  781. *p = '\0';
  782. ++p;
  783. entry->next_range_lower = strtoull(value, 0, 0);
  784. entry->next_range_upper = strtoull(p, 0, 0);
  785. /* validate that upper is greater than lower */
  786. if (entry->next_range_upper <= entry->next_range_lower) {
  787. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  788. "dna_parse_config_entry: Illegal %s "
  789. "setting specified for range %s. Legal "
  790. "format is <lower>-<upper>.\n",
  791. DNA_NEXT_RANGE, entry->dn);
  792. ret = DNA_FAILURE;
  793. entry->next_range_lower = 0;
  794. entry->next_range_upper = 0;
  795. }
  796. /* make sure next range doesn't overlap with
  797. * the active range */
  798. if (((entry->next_range_upper <= entry->maxval) &&
  799. (entry->next_range_upper >= entry->nextval)) ||
  800. ((entry->next_range_lower <= entry->maxval) &&
  801. (entry->next_range_lower >= entry->nextval))) {
  802. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  803. "dna_parse_config_entry: Illegal %s "
  804. "setting specified for range %s. %s "
  805. "overlaps with the active range.\n",
  806. DNA_NEXT_RANGE, entry->dn, DNA_NEXT_RANGE);
  807. ret = DNA_FAILURE;
  808. entry->next_range_lower = 0;
  809. entry->next_range_upper = 0;
  810. }
  811. } else {
  812. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  813. "dna_parse_config_entry: Illegal %s "
  814. "setting specified for range %s. Legal "
  815. "format is <lower>-<upper>.\n",
  816. DNA_NEXT_RANGE, entry->dn);
  817. ret = DNA_FAILURE;
  818. }
  819. slapi_ch_free_string(&value);
  820. }
  821. /* If we were only called to validate config, we can
  822. * just bail out before applying the config changes */
  823. if (apply == 0) {
  824. goto bail;
  825. }
  826. /* Calculate number of remaining values. */
  827. if (entry->next_range_lower != 0) {
  828. entry->remaining = ((entry->next_range_upper - entry->next_range_lower + 1) /
  829. entry->interval) + ((entry->maxval - entry->nextval + 1) /
  830. entry->interval);
  831. } else if (entry->nextval >= entry->maxval) {
  832. entry->remaining = 0;
  833. } else {
  834. entry->remaining = ((entry->maxval - entry->nextval + 1) /
  835. entry->interval);
  836. }
  837. /* create the new value lock for this range */
  838. entry->lock = slapi_new_mutex();
  839. if (!entry->lock) {
  840. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  841. "dna_parse_config_entry: Unable to create lock "
  842. "for range %s.\n", entry->dn);
  843. ret = DNA_FAILURE;
  844. goto bail;
  845. }
  846. /**
  847. * Finally add the entry to the list.
  848. * We sort by scope dn length with longer
  849. * dn's first - this allows the scope
  850. * checking code to be simple and quick and
  851. * cunningly linear.
  852. */
  853. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  854. list = PR_LIST_HEAD(dna_global_config);
  855. while (list != dna_global_config) {
  856. config_entry = (struct configEntry *) list;
  857. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  858. PR_INSERT_BEFORE(&(entry->list), list);
  859. slapi_log_error(SLAPI_LOG_CONFIG,
  860. DNA_PLUGIN_SUBSYSTEM,
  861. "store [%s] before [%s] \n", entry->scope,
  862. config_entry->scope);
  863. entry_added = 1;
  864. break;
  865. }
  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. if (ber_scanf(respber, "{aa}", &lower_str, &upper_str) == LBER_ERROR) {
  1379. ret = LDAP_PROTOCOL_ERROR;
  1380. goto bail;
  1381. }
  1382. }
  1383. /* Fill in upper and lower */
  1384. if (upper_str && lower_str) {
  1385. *upper = strtoull(upper_str, 0, 0);
  1386. *lower = strtoull(lower_str, 0, 0);
  1387. ret = 0;
  1388. } else {
  1389. ret = LDAP_OPERATIONS_ERROR;
  1390. }
  1391. bail:
  1392. if (set_extend_flag) {
  1393. slapi_lock_mutex(config_entry->extend_lock);
  1394. config_entry->extend_in_progress = 0;
  1395. slapi_unlock_mutex(config_entry->extend_lock);
  1396. }
  1397. slapi_ldap_unbind(ld);
  1398. slapi_ch_free_string(&bind_dn);
  1399. slapi_ch_free_string(&bind_passwd);
  1400. slapi_ch_free_string(&bind_method);
  1401. slapi_ch_free_string(&retoid);
  1402. slapi_ch_free_string(&lower_str);
  1403. slapi_ch_free_string(&upper_str);
  1404. ber_free(respber, 1);
  1405. ber_bvfree(request);
  1406. ber_bvfree(responsedata);
  1407. if (ret != 0) {
  1408. *upper = 0;
  1409. *lower = 0;
  1410. }
  1411. return ret;
  1412. }
  1413. static struct berval *dna_create_range_request(char *range_dn)
  1414. {
  1415. struct berval *requestdata = NULL;
  1416. struct berval shared_dn = { 0, NULL };
  1417. BerElement *ber = NULL;
  1418. shared_dn.bv_val = range_dn;
  1419. shared_dn.bv_len = strlen(shared_dn.bv_val);
  1420. if((ber = ber_alloc()) == NULL) {
  1421. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1422. "dna_create_range_request: Error "
  1423. "allocating request data.\n");
  1424. goto bail;
  1425. }
  1426. if (LBER_ERROR == (ber_printf(ber, "{o}", shared_dn.bv_val, shared_dn.bv_len))) {
  1427. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1428. "dna_create_range_request: Error "
  1429. "encoding request data.\n");
  1430. goto bail;
  1431. }
  1432. if (ber_flatten(ber, &requestdata) == -1) {
  1433. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1434. "dna_create_range_request: Error "
  1435. "encoding request data.\n");
  1436. goto bail;
  1437. }
  1438. bail:
  1439. ber_free(ber, 1);
  1440. return requestdata;
  1441. }
  1442. /****************************************************
  1443. Helpers
  1444. ****************************************************/
  1445. static char *dna_get_dn(Slapi_PBlock * pb)
  1446. {
  1447. char *dn = 0;
  1448. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1449. "--> dna_get_dn\n");
  1450. if (slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn)) {
  1451. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1452. "dna_get_dn: failed to get dn of changed entry");
  1453. goto bail;
  1454. }
  1455. bail:
  1456. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1457. "<-- dna_get_dn\n");
  1458. return dn;
  1459. }
  1460. /* config check
  1461. matching config dn or a descendent reloads config
  1462. */
  1463. static int dna_dn_is_config(char *dn)
  1464. {
  1465. int ret = 0;
  1466. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1467. "--> dna_is_config\n");
  1468. if (slapi_dn_issuffix(dn, getPluginDN())) {
  1469. ret = 1;
  1470. }
  1471. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1472. "<-- dna_is_config\n");
  1473. return ret;
  1474. }
  1475. #define DNA_LDAP_TAG_SK_REVERSE 0x81L
  1476. static LDAPControl *dna_build_sort_control(const char *attr)
  1477. {
  1478. LDAPControl *ctrl;
  1479. BerElement *ber;
  1480. int rc;
  1481. ber = ber_alloc();
  1482. if (NULL == ber)
  1483. return NULL;
  1484. rc = ber_printf(ber, "{{stb}}", attr, DNA_LDAP_TAG_SK_REVERSE, 1);
  1485. if (-1 == rc) {
  1486. ber_free(ber, 1);
  1487. return NULL;
  1488. }
  1489. rc = slapi_build_control(LDAP_CONTROL_SORTREQUEST, ber, 1, &ctrl);
  1490. ber_free(ber, 1);
  1491. if (LDAP_SUCCESS != rc)
  1492. return NULL;
  1493. return ctrl;
  1494. }
  1495. /****************************************************
  1496. Functions that actually do things other
  1497. than config and startup
  1498. ****************************************************/
  1499. /*
  1500. * dna_first_free_value()
  1501. *
  1502. * We do search all values between nextval and maxval asking the
  1503. * server to sort them, then we check the first free spot and
  1504. * use it as newval. If we go past the end of the range, we
  1505. * return LDAP_OPERATIONS_ERROR and set newval to be > the
  1506. * maximum configured value for this range. */
  1507. static int
  1508. dna_first_free_value(struct configEntry *config_entry,
  1509. PRUint64 *newval)
  1510. {
  1511. Slapi_Entry **entries = NULL;
  1512. Slapi_PBlock *pb = NULL;
  1513. LDAPControl **ctrls = NULL;
  1514. char *filter = NULL;
  1515. char *prefix;
  1516. int multitype;
  1517. int result, status;
  1518. PRUint64 tmpval, sval, i;
  1519. char *strval = NULL;
  1520. /* check if the config is already out of range */
  1521. if (config_entry->nextval > config_entry->maxval) {
  1522. *newval = config_entry->nextval;
  1523. return LDAP_OPERATIONS_ERROR;
  1524. }
  1525. prefix = config_entry->prefix;
  1526. tmpval = config_entry->nextval;
  1527. if (dna_is_multitype_range(config_entry)) {
  1528. multitype = 1;
  1529. }
  1530. /* We don't sort if we're using a prefix (non integer type) or if this
  1531. * is a multi-type range. Instead, we just search to see if the next
  1532. * value is free, and keep incrementing until we find the next free value. */
  1533. if (prefix || multitype) {
  1534. /* This will allocate the filter string for us. */
  1535. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1536. } else {
  1537. /* This is a single-type range, so just use the first (only)
  1538. * type from the list. */
  1539. ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl));
  1540. if (NULL == ctrls)
  1541. return LDAP_OPERATIONS_ERROR;
  1542. ctrls[0] = dna_build_sort_control(config_entry->types[0]);
  1543. if (NULL == ctrls[0]) {
  1544. slapi_ch_free((void **)&ctrls);
  1545. return LDAP_OPERATIONS_ERROR;
  1546. }
  1547. filter = slapi_ch_smprintf("(&%s(&(%s>=%" NSPRIu64 ")(%s<=%" NSPRIu64 ")))",
  1548. config_entry->filter,
  1549. config_entry->types[0], tmpval,
  1550. config_entry->types[0], config_entry->maxval);
  1551. }
  1552. if (NULL == filter) {
  1553. ldap_controls_free(ctrls);
  1554. ctrls = NULL;
  1555. return LDAP_OPERATIONS_ERROR;
  1556. }
  1557. pb = slapi_pblock_new();
  1558. if (NULL == pb) {
  1559. ldap_controls_free(ctrls);
  1560. ctrls = NULL;
  1561. slapi_ch_free_string(&filter);
  1562. return LDAP_OPERATIONS_ERROR;
  1563. }
  1564. slapi_search_internal_set_pb(pb, config_entry->scope,
  1565. LDAP_SCOPE_SUBTREE, filter,
  1566. config_entry->types, 0, ctrls,
  1567. NULL, getPluginID(), 0);
  1568. slapi_search_internal_pb(pb);
  1569. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1570. if (LDAP_SUCCESS != result) {
  1571. status = LDAP_OPERATIONS_ERROR;
  1572. goto cleanup;
  1573. }
  1574. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1575. &entries);
  1576. if (NULL == entries || NULL == entries[0]) {
  1577. /* no values means we already have a good value */
  1578. *newval = tmpval;
  1579. status = LDAP_SUCCESS;
  1580. goto cleanup;
  1581. }
  1582. if (prefix || multitype) {
  1583. /* The next value identified in the config entry has already
  1584. * been taken. We just iterate through the values until we
  1585. * (hopefully) find a free one. */
  1586. for (tmpval += config_entry->interval; tmpval <= config_entry->maxval;
  1587. tmpval += config_entry->interval) {
  1588. /* This will reuse the old memory for the previous filter. It is
  1589. * guaranteed to have enough space since the filter is the same
  1590. * aside from the assertion value (we allocated enough for the
  1591. * largest supported integer). */
  1592. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1593. /* clear out the pblock so we can re-use it */
  1594. slapi_free_search_results_internal(pb);
  1595. slapi_pblock_init(pb);
  1596. slapi_search_internal_set_pb(pb, config_entry->scope,
  1597. LDAP_SCOPE_SUBTREE, filter,
  1598. config_entry->types, 0, 0,
  1599. NULL, getPluginID(), 0);
  1600. slapi_search_internal_pb(pb);
  1601. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1602. if (LDAP_SUCCESS != result) {
  1603. status = LDAP_OPERATIONS_ERROR;
  1604. goto cleanup;
  1605. }
  1606. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1607. &entries);
  1608. if (NULL == entries || NULL == entries[0]) {
  1609. /* no values means we already have a good value */
  1610. *newval = tmpval;
  1611. status = LDAP_SUCCESS;
  1612. goto cleanup;
  1613. }
  1614. }
  1615. } else {
  1616. /* Entries are sorted and filtered for value >= tval therefore if the
  1617. * first one does not match tval it means that the value is free,
  1618. * otherwise we need to cycle through values until we find a mismatch,
  1619. * the first mismatch is the first free pit. This is guaranteed to
  1620. * be a single-type range, so we can just use the first (only)
  1621. * type from the list of types directly. */
  1622. sval = 0;
  1623. for (i = 0; NULL != entries[i]; i++) {
  1624. strval = slapi_entry_attr_get_charptr(entries[i], config_entry->types[0]);
  1625. errno = 0;
  1626. sval = strtoull(strval, 0, 0);
  1627. if (errno) {
  1628. /* something very wrong here ... */
  1629. status = LDAP_OPERATIONS_ERROR;
  1630. goto cleanup;
  1631. }
  1632. slapi_ch_free_string(&strval);
  1633. if (tmpval != sval)
  1634. break;
  1635. if (config_entry->maxval < sval)
  1636. break;
  1637. tmpval += config_entry->interval;
  1638. }
  1639. }
  1640. /* check if we went past the end of the range */
  1641. if (tmpval <= config_entry->maxval) {
  1642. *newval = tmpval;
  1643. status = LDAP_SUCCESS;
  1644. } else {
  1645. /* we set newval past the end of the range
  1646. * so the caller can easily detect that we
  1647. * overflowed the configured range. */
  1648. *newval = tmpval;
  1649. status = LDAP_OPERATIONS_ERROR;
  1650. }
  1651. cleanup:
  1652. slapi_ch_free_string(&filter);
  1653. slapi_ch_free_string(&strval);
  1654. slapi_free_search_results_internal(pb);
  1655. slapi_pblock_destroy(pb);
  1656. return status;
  1657. }
  1658. /*
  1659. * Perform ldap operationally atomic increment
  1660. * Return the next value to be assigned
  1661. */
  1662. static int dna_get_next_value(struct configEntry *config_entry,
  1663. char **next_value_ret)
  1664. {
  1665. Slapi_PBlock *pb = NULL;
  1666. LDAPMod mod_replace;
  1667. LDAPMod *mods[2];
  1668. char *replace_val[2];
  1669. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1670. char next_value[17];
  1671. PRUint64 setval = 0;
  1672. PRUint64 nextval = 0;
  1673. int ret;
  1674. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1675. "--> dna_get_next_value\n");
  1676. /* get the lock to prevent contention with other threads over
  1677. * the next new value for this range. */
  1678. slapi_lock_mutex(config_entry->lock);
  1679. /* get the first value */
  1680. ret = dna_first_free_value(config_entry, &setval);
  1681. if (LDAP_SUCCESS != ret) {
  1682. /* check if we overflowed the configured range */
  1683. if (setval > config_entry->maxval) {
  1684. /* try for a new range or fail */
  1685. ret = dna_fix_maxval(config_entry);
  1686. if (LDAP_SUCCESS != ret) {
  1687. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1688. "dna_get_next_value: no more values available!!\n");
  1689. goto done;
  1690. }
  1691. /* get the first value from our newly extended range */
  1692. ret = dna_first_free_value(config_entry, &setval);
  1693. if (LDAP_SUCCESS != ret)
  1694. goto done;
  1695. } else {
  1696. /* dna_first_free_value() failed for some unknown reason */
  1697. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1698. "dna_get_next_value: failed to allocate a new ID!!\n");
  1699. goto done;
  1700. }
  1701. }
  1702. nextval = setval + config_entry->interval;
  1703. /* update nextval if we have not reached the end
  1704. * of our current range */
  1705. if ((config_entry->maxval == -1) ||
  1706. (nextval <= (config_entry->maxval + config_entry->interval))) {
  1707. /* try to set the new next value in the config entry */
  1708. PR_snprintf(next_value, sizeof(next_value),"%" NSPRIu64, nextval);
  1709. /* set up our replace modify operation */
  1710. replace_val[0] = next_value;
  1711. replace_val[1] = 0;
  1712. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1713. mod_replace.mod_type = DNA_NEXTVAL;
  1714. mod_replace.mod_values = replace_val;
  1715. mods[0] = &mod_replace;
  1716. mods[1] = 0;
  1717. pb = slapi_pblock_new();
  1718. if (NULL == pb) {
  1719. ret = LDAP_OPERATIONS_ERROR;
  1720. goto done;
  1721. }
  1722. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1723. mods, 0, 0, getPluginID(), 0);
  1724. slapi_modify_internal_pb(pb);
  1725. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1726. }
  1727. if (LDAP_SUCCESS == ret) {
  1728. slapi_ch_free_string(next_value_ret);
  1729. *next_value_ret = slapi_ch_smprintf("%" NSPRIu64, setval);
  1730. if (NULL == *next_value_ret) {
  1731. ret = LDAP_OPERATIONS_ERROR;
  1732. goto done;
  1733. }
  1734. /* update our cached config */
  1735. dna_notice_allocation(config_entry, nextval, setval, 1);
  1736. }
  1737. done:
  1738. slapi_unlock_mutex(config_entry->lock);
  1739. if (pb) {
  1740. slapi_pblock_destroy(pb);
  1741. }
  1742. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1743. "<-- dna_get_next_value\n");
  1744. return ret;
  1745. }
  1746. /*
  1747. * dna_update_shared_config()
  1748. *
  1749. * Updates the shared config entry if one is
  1750. * configured. Returns the LDAP result code.
  1751. *
  1752. * The lock for configEntry should be obtained
  1753. * before calling this function.
  1754. * */
  1755. static int
  1756. dna_update_shared_config(struct configEntry * config_entry)
  1757. {
  1758. int ret = LDAP_SUCCESS;
  1759. if (config_entry && config_entry->shared_cfg_dn) {
  1760. /* Update the shared config entry if one is configured */
  1761. Slapi_PBlock *pb = NULL;
  1762. LDAPMod mod_replace;
  1763. LDAPMod *mods[2];
  1764. char *replace_val[2];
  1765. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1766. char remaining_vals[17];
  1767. /* We store the number of remaining assigned values
  1768. * in the shared config entry. */
  1769. PR_snprintf(remaining_vals, sizeof(remaining_vals),"%" NSPRIu64, config_entry->remaining);
  1770. /* set up our replace modify operation */
  1771. replace_val[0] = remaining_vals;
  1772. replace_val[1] = 0;
  1773. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1774. mod_replace.mod_type = DNA_REMAINING;
  1775. mod_replace.mod_values = replace_val;
  1776. mods[0] = &mod_replace;
  1777. mods[1] = 0;
  1778. pb = slapi_pblock_new();
  1779. if (NULL == pb) {
  1780. ret = LDAP_OPERATIONS_ERROR;
  1781. } else {
  1782. slapi_modify_internal_set_pb(pb, config_entry->shared_cfg_dn,
  1783. mods, NULL, NULL, getPluginID(), 0);
  1784. slapi_modify_internal_pb(pb);
  1785. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1786. /* If the shared config for this instance doesn't
  1787. * already exist, we add it. */
  1788. if (ret == LDAP_NO_SUCH_OBJECT) {
  1789. Slapi_Entry *e = NULL;
  1790. /* Set up the new shared config entry */
  1791. e = slapi_entry_alloc();
  1792. /* the entry now owns the dup'd dn */
  1793. slapi_entry_init(e, slapi_ch_strdup(config_entry->shared_cfg_dn), NULL);
  1794. slapi_entry_add_string(e, SLAPI_ATTR_OBJECTCLASS, "extensibleObject");
  1795. slapi_entry_add_string(e, DNA_HOSTNAME, hostname);
  1796. slapi_entry_add_string(e, DNA_PORTNUM, portnum);
  1797. if (secureportnum) {
  1798. slapi_entry_add_string(e, DNA_SECURE_PORTNUM, secureportnum);
  1799. }
  1800. slapi_entry_add_string(e, DNA_REMAINING, remaining_vals);
  1801. /* clear pb for re-use */
  1802. slapi_pblock_init(pb);
  1803. /* e will be consumed by slapi_add_internal() */
  1804. slapi_add_entry_internal_set_pb(pb, e, NULL, getPluginID(), 0);
  1805. slapi_add_internal_pb(pb);
  1806. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1807. }
  1808. if (ret != LDAP_SUCCESS) {
  1809. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1810. "dna_update_shared_config: Unable to update shared config entry: %s [error %d]\n",
  1811. config_entry->shared_cfg_dn, ret);
  1812. }
  1813. slapi_pblock_destroy(pb);
  1814. pb = NULL;
  1815. }
  1816. }
  1817. return ret;
  1818. }
  1819. /*
  1820. * dna_update_next_range()
  1821. *
  1822. * Sets the proper value for the next range in
  1823. * all configuration entries and in-memory cache.
  1824. *
  1825. * The range you are updating should be locked
  1826. * before calling this function.
  1827. */
  1828. static int
  1829. dna_update_next_range(struct configEntry *config_entry,
  1830. PRUint64 lower, PRUint64 upper)
  1831. {
  1832. Slapi_PBlock *pb = NULL;
  1833. LDAPMod mod_replace;
  1834. LDAPMod *mods[2];
  1835. char *replace_val[2];
  1836. /* 32 for the two numbers, 1 for the '-', and one for the '\0' */
  1837. char nextrange_value[34];
  1838. int ret = 0;
  1839. /* Try to set the new next range in the config entry. */
  1840. PR_snprintf(nextrange_value, sizeof(nextrange_value), "%" NSPRIu64 "-%" NSPRIu64,
  1841. lower, upper);
  1842. /* set up our replace modify operation */
  1843. replace_val[0] = nextrange_value;
  1844. replace_val[1] = 0;
  1845. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1846. mod_replace.mod_type = DNA_NEXT_RANGE;
  1847. mod_replace.mod_values = replace_val;
  1848. mods[0] = &mod_replace;
  1849. mods[1] = 0;
  1850. pb = slapi_pblock_new();
  1851. if (NULL == pb) {
  1852. ret = LDAP_OPERATIONS_ERROR;
  1853. goto bail;
  1854. }
  1855. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1856. mods, 0, 0, getPluginID(), 0);
  1857. slapi_modify_internal_pb(pb);
  1858. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1859. slapi_pblock_destroy(pb);
  1860. pb = NULL;
  1861. if (ret != LDAP_SUCCESS) {
  1862. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1863. "dna_update_next_range: Error updating "
  1864. "configuration entry [err=%d]\n", ret);
  1865. } else {
  1866. /* update the cached config and the shared config */
  1867. config_entry->next_range_lower = lower;
  1868. config_entry->next_range_upper = upper;
  1869. dna_notice_allocation(config_entry, 0, 0, 0);
  1870. }
  1871. bail:
  1872. return ret;
  1873. }
  1874. /* dna_activate_next_range()
  1875. *
  1876. * Makes the next range the active range. This
  1877. * will update the config entry, the in-memory
  1878. * config info, and the shared config entry.
  1879. *
  1880. * The lock for configEntry should
  1881. * be obtained before calling this function.
  1882. */
  1883. static int
  1884. dna_activate_next_range(struct configEntry *config_entry)
  1885. {
  1886. Slapi_PBlock *pb = NULL;
  1887. LDAPMod mod_maxval;
  1888. LDAPMod mod_nextval;
  1889. LDAPMod mod_nextrange;
  1890. LDAPMod *mods[4];
  1891. char *maxval_vals[2];
  1892. char *nextval_vals[2];
  1893. char *nextrange_vals[1];
  1894. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1895. char maxval_val[17];
  1896. char nextval_val[17];
  1897. int ret = 0;
  1898. /* Setup the modify operation for the config entry */
  1899. PR_snprintf(maxval_val, sizeof(maxval_val),"%" NSPRIu64, config_entry->next_range_upper);
  1900. PR_snprintf(nextval_val, sizeof(nextval_val),"%" NSPRIu64, config_entry->next_range_lower);
  1901. maxval_vals[0] = maxval_val;
  1902. maxval_vals[1] = 0;
  1903. nextval_vals[0] = nextval_val;
  1904. nextval_vals[1] = 0;
  1905. nextrange_vals[0] = 0;
  1906. mod_maxval.mod_op = LDAP_MOD_REPLACE;
  1907. mod_maxval.mod_type = DNA_MAXVAL;
  1908. mod_maxval.mod_values = maxval_vals;
  1909. mod_nextval.mod_op = LDAP_MOD_REPLACE;
  1910. mod_nextval.mod_type = DNA_NEXTVAL;
  1911. mod_nextval.mod_values = nextval_vals;
  1912. mod_nextrange.mod_op = LDAP_MOD_DELETE;
  1913. mod_nextrange.mod_type = DNA_NEXT_RANGE;
  1914. mod_nextrange.mod_values = nextrange_vals;
  1915. mods[0] = &mod_maxval;
  1916. mods[1] = &mod_nextval;
  1917. mods[2] = &mod_nextrange;
  1918. mods[3] = 0;
  1919. /* Update the config entry first */
  1920. pb = slapi_pblock_new();
  1921. if (NULL == pb) {
  1922. ret = LDAP_OPERATIONS_ERROR;
  1923. goto bail;
  1924. }
  1925. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1926. mods, 0, 0, getPluginID(), 0);
  1927. slapi_modify_internal_pb(pb);
  1928. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1929. slapi_pblock_destroy(pb);
  1930. pb = NULL;
  1931. if (ret != LDAP_SUCCESS) {
  1932. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1933. "dna_activate_next_range: Error updating "
  1934. "configuration entry [err=%d]\n", ret);
  1935. } else {
  1936. /* Update the in-memory config info */
  1937. config_entry->maxval = config_entry->next_range_upper;
  1938. config_entry->nextval = config_entry->next_range_lower;
  1939. config_entry->next_range_upper = 0;
  1940. config_entry->next_range_lower = 0;
  1941. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1942. config_entry->interval);
  1943. /* update the shared configuration */
  1944. dna_update_shared_config(config_entry);
  1945. }
  1946. bail:
  1947. return ret;
  1948. }
  1949. /*
  1950. * dna_is_replica_bind_dn()
  1951. *
  1952. * Checks if the passed in DN is the replica bind DN. This
  1953. * is used to check if a user is allowed to request range
  1954. * from us.
  1955. *
  1956. * Returns 1 if bind_dn matches the replica bind dn, 0 otherwise. */
  1957. static int dna_is_replica_bind_dn(char *range_dn, char *bind_dn)
  1958. {
  1959. char *replica_dn = NULL;
  1960. Slapi_DN *replica_sdn = NULL;
  1961. char *replica_bind_dn = NULL;
  1962. Slapi_DN *replica_bind_sdn = NULL;
  1963. Slapi_DN *range_sdn = NULL;
  1964. Slapi_DN *bind_sdn = NULL;
  1965. Slapi_Entry *e = NULL;
  1966. char *attrs[2];
  1967. Slapi_Backend *be = NULL;
  1968. const char *be_suffix = NULL;
  1969. int ret = 0;
  1970. /* Find the backend suffix where the shared config is stored. */
  1971. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  1972. if ((be = slapi_be_select(range_sdn)) != NULL) {
  1973. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  1974. }
  1975. /* Fetch the "cn=replica" entry for the backend that stores
  1976. * the shared config. We need to see what the configured
  1977. * replica bind DN is. */
  1978. if (be_suffix) {
  1979. /* This function converts the old DN style to the new one. */
  1980. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  1981. if (NULL == replica_dn) {
  1982. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1983. "dna_is_replica_bind_dn: failed to create "
  1984. "replica dn for %s\n", be_suffix);
  1985. ret = 1;
  1986. goto done;
  1987. }
  1988. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  1989. attrs[0] = DNA_REPL_BIND_DN;
  1990. attrs[1] = 0;
  1991. /* Find cn=replica entry via search */
  1992. slapi_search_internal_get_entry(replica_sdn, attrs, &e, getPluginID());
  1993. if (e) {
  1994. replica_bind_dn = slapi_entry_attr_get_charptr(e, DNA_REPL_BIND_DN);
  1995. } else {
  1996. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1997. "dna_is_replica_bind_dn: Failed to fetch replica entry "
  1998. "for range %s\n", range_dn);
  1999. }
  2000. }
  2001. if (replica_bind_dn) {
  2002. /* Compare the passed in bind dn to the replica bind dn */
  2003. bind_sdn = slapi_sdn_new_dn_byref(bind_dn);
  2004. replica_bind_sdn = slapi_sdn_new_dn_passin(replica_bind_dn);
  2005. if (slapi_sdn_compare(bind_sdn, replica_bind_sdn) == 0) {
  2006. ret = 1;
  2007. }
  2008. }
  2009. done:
  2010. slapi_entry_free(e);
  2011. slapi_sdn_free(&range_sdn);
  2012. slapi_sdn_free(&replica_sdn);
  2013. slapi_sdn_free(&replica_bind_sdn);
  2014. slapi_sdn_free(&bind_sdn);
  2015. return ret;
  2016. }
  2017. static int dna_get_replica_bind_creds(char *range_dn, struct dnaServer *server,
  2018. char **bind_dn, char **bind_passwd,
  2019. char **bind_method, int *is_ssl, int *port)
  2020. {
  2021. Slapi_PBlock *pb = NULL;
  2022. Slapi_DN *range_sdn = NULL;
  2023. char *replica_dn = NULL;
  2024. Slapi_Backend *be = NULL;
  2025. const char *be_suffix = NULL;
  2026. char *attrs[6];
  2027. char *filter = NULL;
  2028. char *bind_cred = NULL;
  2029. char *transport = NULL;
  2030. Slapi_Entry **entries = NULL;
  2031. int ret = LDAP_OPERATIONS_ERROR;
  2032. /* Find the backend suffix where the shared config is stored. */
  2033. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2034. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2035. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2036. }
  2037. /* Fetch the replication agreement entry */
  2038. if (be_suffix) {
  2039. /* This function converts the old DN style to the new one. */
  2040. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2041. if (NULL == replica_dn) {
  2042. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2043. "dna_get_replica_bind_creds: failed to create "
  2044. "replica dn for %s\n", be_suffix);
  2045. ret = LDAP_PARAM_ERROR;
  2046. goto bail;
  2047. }
  2048. filter = slapi_ch_smprintf("(&(nsds5ReplicaHost=%s)(|(" DNA_REPL_PORT "=%u)"
  2049. "(" DNA_REPL_PORT "=%u)))",
  2050. server->host, server->port, server->secureport);
  2051. attrs[0] = DNA_REPL_BIND_DN;
  2052. attrs[1] = DNA_REPL_CREDS;
  2053. attrs[2] = DNA_REPL_BIND_METHOD;
  2054. attrs[3] = DNA_REPL_TRANSPORT;
  2055. attrs[4] = DNA_REPL_PORT;
  2056. attrs[5] = 0;
  2057. pb = slapi_pblock_new();
  2058. if (NULL == pb) {
  2059. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2060. "dna_get_replica_bind_creds: Failed to "
  2061. "allocate pblock\n");
  2062. goto bail;
  2063. }
  2064. slapi_search_internal_set_pb(pb, replica_dn,
  2065. LDAP_SCOPE_ONELEVEL, filter,
  2066. attrs, 0, NULL, NULL, getPluginID(), 0);
  2067. slapi_search_internal_pb(pb);
  2068. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2069. if (LDAP_SUCCESS != ret) {
  2070. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2071. "dna_get_replica_bind_creds: Failed to fetch replica "
  2072. "bind credentials for range %s, server %s, port %u [error %d]\n",
  2073. range_dn, server->host, server->port, ret);
  2074. goto bail;
  2075. }
  2076. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2077. &entries);
  2078. if (NULL == entries || NULL == entries[0]) {
  2079. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2080. "dna_get_replica_bind_creds: Failed to fetch replication "
  2081. "agreement for range %s, server %s, port %u\n", range_dn,
  2082. server->host, server->port);
  2083. ret = LDAP_OPERATIONS_ERROR;
  2084. goto bail;
  2085. }
  2086. /* Get the replication bind dn and password from the agreement. It
  2087. * is up to the caller to free these when they are finished. */
  2088. slapi_ch_free_string(bind_dn);
  2089. slapi_ch_free_string(bind_method);
  2090. *bind_dn = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_DN);
  2091. *bind_method = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_METHOD);
  2092. bind_cred = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_CREDS);
  2093. transport = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_TRANSPORT);
  2094. *port = slapi_entry_attr_get_int(entries[0], DNA_REPL_PORT);
  2095. /* Check if we should use SSL */
  2096. if (transport && (strcasecmp(transport, "SSL") == 0)) {
  2097. *is_ssl = 1;
  2098. } else if (transport && (strcasecmp(transport, "TLS") == 0)) {
  2099. *is_ssl = 2;
  2100. } else {
  2101. *is_ssl = 0;
  2102. }
  2103. /* fix up the bind method */
  2104. if ((NULL == *bind_method) || (strcasecmp(*bind_method, "SIMPLE") == 0)) {
  2105. slapi_ch_free_string(bind_method);
  2106. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2107. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2108. slapi_ch_free_string(bind_method);
  2109. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2110. } else if (strcasecmp(*bind_method, "SASL/GSSAPI") == 0) {
  2111. slapi_ch_free_string(bind_method);
  2112. *bind_method = slapi_ch_strdup("GSSAPI");
  2113. } else if (strcasecmp(*bind_method, "SASL/DIGEST-MD5") == 0) {
  2114. slapi_ch_free_string(bind_method);
  2115. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2116. } else { /* some other weird value */
  2117. ; /* just use it directly */
  2118. }
  2119. /* Decode the password */
  2120. if (bind_cred) {
  2121. int pw_ret = 0;
  2122. slapi_ch_free_string(bind_passwd);
  2123. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2124. if (pw_ret == -1) {
  2125. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2126. "dna_get_replica_bind_creds: Failed to decode "
  2127. "replica bind credentials for range %s, server %s, "
  2128. "port %u\n", range_dn, server->host, server->port);
  2129. goto bail;
  2130. } else if (pw_ret != 0) {
  2131. /* The password was already in clear text, so pw_rever_decode
  2132. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2133. * to prevent a double free. The memory is now owned by
  2134. * bind_passwd, which is the callers responsibility to free. */
  2135. bind_cred = NULL;
  2136. }
  2137. }
  2138. }
  2139. /* If we didn't get both a bind DN and a decoded password,
  2140. * then just free everything and return an error. */
  2141. if (*bind_dn && *bind_passwd) {
  2142. ret = 0;
  2143. } else {
  2144. slapi_ch_free_string(bind_dn);
  2145. slapi_ch_free_string(bind_passwd);
  2146. slapi_ch_free_string(bind_method);
  2147. }
  2148. bail:
  2149. slapi_ch_free_string(&transport);
  2150. slapi_ch_free_string(&filter);
  2151. slapi_sdn_free(&range_sdn);
  2152. slapi_ch_free_string(&replica_dn);
  2153. slapi_ch_free_string(&bind_cred);
  2154. slapi_free_search_results_internal(pb);
  2155. slapi_pblock_destroy(pb);
  2156. return ret;
  2157. }
  2158. /*
  2159. * dna_list_contains_type()
  2160. *
  2161. * Checks if a type is contained in a list of types.
  2162. * Returns 1 if the type is found, 0 otherwise.
  2163. */
  2164. static int
  2165. dna_list_contains_type(char **list, char *type)
  2166. {
  2167. int ret = 0;
  2168. int i = 0;
  2169. if (list && type) {
  2170. for (i = 0; list[i]; i++) {
  2171. if (slapi_attr_types_equivalent(type, list[i])) {
  2172. ret = 1;
  2173. break;
  2174. }
  2175. }
  2176. }
  2177. return ret;
  2178. }
  2179. /*
  2180. * dna_list_contains_types()
  2181. *
  2182. * Checks if all types in one list (types) are contained
  2183. * in another list of types (list). Returns 1 if all
  2184. * types are found, 0 otherwise.
  2185. */
  2186. static int
  2187. dna_list_contains_types(char **list, char **types)
  2188. {
  2189. int ret = 1;
  2190. int i = 0;
  2191. int j = 0;
  2192. if (list && types) {
  2193. for (i = 0; types[i]; i++) {
  2194. int found = 0;
  2195. for (j = 0; list[j]; j++) {
  2196. if (slapi_attr_types_equivalent(types[i], list[i])) {
  2197. found = 1;
  2198. break;
  2199. }
  2200. }
  2201. if (!found) {
  2202. ret = 0;
  2203. break;
  2204. }
  2205. }
  2206. } else {
  2207. ret = 0;
  2208. }
  2209. return ret;
  2210. }
  2211. /*
  2212. * dna_list_remove_type()
  2213. *
  2214. * Removes a type from a list of types.
  2215. */
  2216. static void
  2217. dna_list_remove_type(char **list, char *type)
  2218. {
  2219. int i = 0;
  2220. int found_type = 0;
  2221. if (list && type) {
  2222. /* Go through the list until we find the type that
  2223. * we want to remove. We simply free the type we
  2224. * want to remove and shift the remaining array
  2225. * elements down by one index. This will leave us
  2226. * with two NULL elements at the end of the list,
  2227. * but this should not cause any problems. */
  2228. for (i = 0; list[i]; i++) {
  2229. if (found_type) {
  2230. list[i] = list[i + 1];
  2231. } else if (slapi_attr_types_equivalent(type, list[i])) {
  2232. slapi_ch_free_string(&list[i]);
  2233. list[i] = list[i + 1];
  2234. found_type = 1;
  2235. }
  2236. }
  2237. }
  2238. }
  2239. /*
  2240. * dna_is_multitype_range()
  2241. *
  2242. * Returns 1 if the range has multiple types configured.
  2243. * Returns 0 otherwise.
  2244. */
  2245. static int dna_is_multitype_range(struct configEntry *config_entry)
  2246. {
  2247. int ret = 0;
  2248. if (config_entry && config_entry->types && config_entry->types[1]) {
  2249. ret = 1;
  2250. }
  2251. return ret;
  2252. }
  2253. /*
  2254. * dna_create_valcheck_filter()
  2255. *
  2256. * Creates a filter string used to check if a value is free. If
  2257. * filter already holds a valid pointer, it is assumed to have enough
  2258. * space to hold the filter. This allows the same memory to be used
  2259. * over and over when you only want to change the assertion value.
  2260. * If filter contains a NULL pointer, a new string of the appropriate
  2261. * size will be allocated. The caller must free this when finished.
  2262. */
  2263. static void
  2264. dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter)
  2265. {
  2266. int filterlen = 0;
  2267. int typeslen = 0;
  2268. int i = 0;
  2269. int bytes_out = 0;
  2270. int multitype = 0;
  2271. /* Just return if we didn't get an address for the filter. */
  2272. if (filter == NULL) {
  2273. return;
  2274. }
  2275. /* To determine the filter length, we add together the following:
  2276. *
  2277. * - the string length of the filter in the config
  2278. * - the string length sum of all configured types
  2279. * - 23 bytes for each type (20 for the max string
  2280. * representation of a NSPRIu64, 3 for "(=)"
  2281. * - 3 bytes for the beginning and end of the filter - "(&" and ")"
  2282. * - 3 bytes to OR together multiple types (if present) - "(|" and ")"
  2283. * - the string length of the prefix (if one is configured) for each type
  2284. * - 1 byte for the trailing \0
  2285. *
  2286. * The filter length should be the same every time if the config struct
  2287. * has not been changed. We need to calculate the filter length even
  2288. * if we are reusing memory since we have no other way of knowing the
  2289. * length used when it was originally allocated. We trust the caller
  2290. * to only reuse the pointer with the same config struct.
  2291. */
  2292. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2293. typeslen += strlen(config_entry->types[i]);
  2294. }
  2295. if (i > 1) {
  2296. multitype = 1;
  2297. }
  2298. filterlen = strlen(config_entry->filter) + typeslen +
  2299. (i * 23) + 3 + 1 +
  2300. (config_entry->prefix ? (i * strlen(config_entry->prefix)) : 0) +
  2301. (multitype ? 3 : 0);
  2302. /* Allocate space for the filter if it hasn't been allocated yet. */
  2303. if (*filter == NULL) {
  2304. *filter = slapi_ch_malloc(filterlen);
  2305. }
  2306. /* Write out the beginning of the filter. If multiple types
  2307. * are configured, we need to OR together the search clauses
  2308. * for the types. */
  2309. if (multitype) {
  2310. bytes_out = snprintf(*filter, filterlen, "(&%s(|", config_entry->filter);
  2311. } else {
  2312. bytes_out = snprintf(*filter, filterlen, "(&%s", config_entry->filter);
  2313. }
  2314. /* Loop through the types and append each filter clause. */
  2315. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2316. bytes_out += snprintf(*filter + bytes_out, filterlen - bytes_out,
  2317. "(%s=%s%" PRIu64 ")", config_entry->types[i],
  2318. config_entry->prefix ? config_entry->prefix : "",
  2319. value);
  2320. }
  2321. /* Append the end of the filter. We need an extra paren
  2322. * to close out the OR if we have multiple types. */
  2323. if (multitype) {
  2324. strncat(*filter, "))", filterlen - bytes_out);
  2325. } else {
  2326. strncat(*filter, ")", filterlen - bytes_out);
  2327. }
  2328. }
  2329. /* for mods and adds:
  2330. where dn's are supplied, the closest in scope
  2331. is used as long as the type filter matches
  2332. and the type has not been generated yet.
  2333. */
  2334. static int dna_pre_op(Slapi_PBlock * pb, int modtype)
  2335. {
  2336. char *dn = 0;
  2337. PRCList *list = 0;
  2338. struct configEntry *config_entry = 0;
  2339. struct slapi_entry *e = 0;
  2340. Slapi_Entry *resulting_e = 0;
  2341. char *value = 0;
  2342. char **types_to_generate = NULL;
  2343. char **generated_types = NULL;
  2344. Slapi_Mods *smods = 0;
  2345. Slapi_Mod *smod = 0;
  2346. LDAPMod **mods;
  2347. int free_entry = 0;
  2348. char *errstr = NULL;
  2349. int i = 0;
  2350. int ret = 0;
  2351. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2352. "--> dna_pre_op\n");
  2353. /* Just bail if we aren't ready to service requests yet. */
  2354. if (!g_plugin_started)
  2355. goto bail;
  2356. if (0 == (dn = dna_get_dn(pb)))
  2357. goto bail;
  2358. if (LDAP_CHANGETYPE_ADD == modtype) {
  2359. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  2360. } else {
  2361. /* xxxPAR: Ideally SLAPI_MODIFY_EXISTING_ENTRY should be
  2362. * available but it turns out that is only true if you are
  2363. * a dbm backend pre-op plugin - lucky dbm backend pre-op
  2364. * plugins.
  2365. * I think that is wrong since the entry is useful for filter
  2366. * tests and schema checks and this plugin shouldn't be limited
  2367. * to a single backend type, but I don't want that fight right
  2368. * now so we go get the entry here
  2369. *
  2370. slapi_pblock_get( pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  2371. */
  2372. Slapi_DN *tmp_dn = slapi_sdn_new_dn_byref(dn);
  2373. if (tmp_dn) {
  2374. slapi_search_internal_get_entry(tmp_dn, 0, &e, getPluginID());
  2375. slapi_sdn_free(&tmp_dn);
  2376. free_entry = 1;
  2377. }
  2378. /* grab the mods - we'll put them back later with
  2379. * our modifications appended
  2380. */
  2381. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  2382. smods = slapi_mods_new();
  2383. slapi_mods_init_passin(smods, mods);
  2384. /* We need the resulting entry after the mods are applied to
  2385. * see if the entry is within the scope. */
  2386. if (e) {
  2387. resulting_e = slapi_entry_dup(e);
  2388. if (mods && (slapi_entry_apply_mods(resulting_e, mods) != LDAP_SUCCESS)) {
  2389. /* The mods don't apply cleanly, so we just let this op go
  2390. * to let the main server handle it. */
  2391. goto bailmod;
  2392. }
  2393. }
  2394. }
  2395. if (0 == e)
  2396. goto bailmod;
  2397. if (dna_dn_is_config(dn)) {
  2398. /* Validate config changes, but don't apply them.
  2399. * This allows us to reject invalid config changes
  2400. * here at the pre-op stage. Applying the config
  2401. * needs to be done at the post-op stage. */
  2402. Slapi_Entry *test_e = NULL;
  2403. /* For a MOD, we need to check the resulting entry */
  2404. if (LDAP_CHANGETYPE_ADD == modtype) {
  2405. test_e = e;
  2406. } else {
  2407. test_e = resulting_e;
  2408. }
  2409. if (dna_parse_config_entry(test_e, 0) != DNA_SUCCESS) {
  2410. /* Refuse the operation if config parsing failed. */
  2411. ret = LDAP_UNWILLING_TO_PERFORM;
  2412. if (LDAP_CHANGETYPE_ADD == modtype) {
  2413. errstr = slapi_ch_smprintf("Not a valid DNA configuration entry.");
  2414. } else {
  2415. errstr = slapi_ch_smprintf("Changes result in an invalid "
  2416. "DNA configuration.");
  2417. }
  2418. }
  2419. /* We're done, so just bail. */
  2420. goto bailmod;
  2421. }
  2422. dna_read_lock();
  2423. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2424. list = PR_LIST_HEAD(dna_global_config);
  2425. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  2426. config_entry = (struct configEntry *) list;
  2427. /* Did we already service all of these configured types? */
  2428. if (dna_list_contains_types(generated_types, config_entry->types)) {
  2429. goto next;
  2430. }
  2431. /* is the entry in scope? */
  2432. if (config_entry->scope) {
  2433. if (!slapi_dn_issuffix(dn, config_entry->scope))
  2434. goto next;
  2435. }
  2436. /* does the entry match the filter? */
  2437. if (config_entry->slapi_filter) {
  2438. Slapi_Entry *test_e = NULL;
  2439. /* For a MOD operation, we need to check the filter
  2440. * against the resulting entry. */
  2441. if (LDAP_CHANGETYPE_ADD == modtype) {
  2442. test_e = e;
  2443. } else {
  2444. test_e = resulting_e;
  2445. }
  2446. if (LDAP_SUCCESS != slapi_vattr_filter_test(pb,
  2447. test_e,
  2448. config_entry->
  2449. slapi_filter, 0))
  2450. goto next;
  2451. }
  2452. if (LDAP_CHANGETYPE_ADD == modtype) {
  2453. if (dna_is_multitype_range(config_entry)) {
  2454. /* For a multi-type range, we only generate a value
  2455. * for types where the magic value is set. We do not
  2456. * generate a value for missing types. */
  2457. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2458. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  2459. if (value && !slapi_UTF8CASECMP(config_entry->generate, value)) {
  2460. slapi_ch_array_add(&types_to_generate,
  2461. slapi_ch_strdup(config_entry->types[i]));
  2462. }
  2463. slapi_ch_free_string(&value);
  2464. }
  2465. } else {
  2466. /* For a single type range, we generate the value if
  2467. * the magic value is set or if the type is missing. */
  2468. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  2469. if ((value && !slapi_UTF8CASECMP(config_entry->generate, value))
  2470. || 0 == value) {
  2471. slapi_ch_array_add(&types_to_generate,
  2472. slapi_ch_strdup(config_entry->types[0]));
  2473. }
  2474. slapi_ch_free_string(&value);
  2475. }
  2476. } else {
  2477. /* check mods for magic value */
  2478. Slapi_Mod *next_mod = slapi_mod_new();
  2479. smod = slapi_mods_get_first_smod(smods, next_mod);
  2480. while (smod) {
  2481. char *type = (char *)
  2482. slapi_mod_get_type(smod);
  2483. /* See if the type matches any configured type. */
  2484. if (dna_list_contains_type(config_entry->types, type)) {
  2485. /* If all values are being deleted, we need to
  2486. * generate a new value. We don't do this for
  2487. * multi-type ranges since they require the magic
  2488. * value to be specified to trigger generation. */
  2489. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  2490. !dna_is_multitype_range(config_entry)) {
  2491. int numvals = slapi_mod_get_num_values(smod);
  2492. if (numvals == 0) {
  2493. slapi_ch_array_add(&types_to_generate,
  2494. slapi_ch_strdup(type));
  2495. } else {
  2496. Slapi_Attr *attr = NULL;
  2497. int e_numvals = 0;
  2498. slapi_entry_attr_find(e, type, &attr);
  2499. if (attr) {
  2500. slapi_attr_get_numvalues(attr, &e_numvals);
  2501. if (numvals >= e_numvals) {
  2502. slapi_ch_array_add(&types_to_generate,
  2503. slapi_ch_strdup(type));
  2504. }
  2505. }
  2506. }
  2507. } else {
  2508. /* This is either adding or replacing a value */
  2509. struct berval *bv = slapi_mod_get_first_value(smod);
  2510. /* If this type is already in the to be generated
  2511. * list, a previous mod in this same modify operation
  2512. * either removed all values or set the magic value.
  2513. * It's possible that this mod is adding a valid value,
  2514. * which means we would not want to generate a new value.
  2515. * It is safe to remove this type from the to be
  2516. * generated list since it will be re-added here if
  2517. * necessary. */
  2518. if (dna_list_contains_type(types_to_generate, type)) {
  2519. dna_list_remove_type(types_to_generate, type);
  2520. }
  2521. /* If we have a value, see if it's the magic value. */
  2522. if (bv) {
  2523. int len = strlen(config_entry->generate);
  2524. if (len == bv->bv_len) {
  2525. if (!slapi_UTF8NCASECMP(bv->bv_val,
  2526. config_entry->generate,
  2527. len)) {
  2528. slapi_ch_array_add(&types_to_generate,
  2529. slapi_ch_strdup(type));
  2530. }
  2531. }
  2532. } else if (!dna_is_multitype_range(config_entry)) {
  2533. /* This is a replace with no new values, so we need
  2534. * to generate a new value if this is not a multi-type
  2535. * range. */
  2536. slapi_ch_array_add(&types_to_generate,
  2537. slapi_ch_strdup(type));
  2538. }
  2539. }
  2540. }
  2541. slapi_mod_done(next_mod);
  2542. smod = slapi_mods_get_next_smod(smods, next_mod);
  2543. }
  2544. slapi_mod_free(&next_mod);
  2545. }
  2546. /* We need to perform one last check for modify operations. If an
  2547. * entry within the scope has not triggered generation yet, we need
  2548. * to see if a value exists for the managed type in the resulting
  2549. * entry. This will catch a modify operation that brings an entry
  2550. * into scope for a managed range, but doesn't supply a value for
  2551. * the managed type. We don't do this for multi-type ranges. */
  2552. if ((LDAP_CHANGETYPE_MODIFY == modtype) && (!types_to_generate ||
  2553. (types_to_generate && !types_to_generate[0])) &&
  2554. !dna_is_multitype_range(config_entry)) {
  2555. Slapi_Attr *attr = NULL;
  2556. if (slapi_entry_attr_find(resulting_e, config_entry->types[0], &attr) != 0) {
  2557. slapi_ch_array_add(&types_to_generate,
  2558. slapi_ch_strdup(config_entry->types[0]));
  2559. }
  2560. }
  2561. if (types_to_generate && types_to_generate[0]) {
  2562. char *new_value;
  2563. int len;
  2564. /* create the value to add */
  2565. ret = dna_get_next_value(config_entry, &value);
  2566. if (DNA_SUCCESS != ret) {
  2567. errstr = slapi_ch_smprintf("Allocation of a new value for range"
  2568. " %s failed! Unable to proceed.",
  2569. config_entry->dn);
  2570. break;
  2571. }
  2572. len = strlen(value) + 1;
  2573. if (config_entry->prefix) {
  2574. len += strlen(config_entry->prefix);
  2575. }
  2576. new_value = slapi_ch_malloc(len);
  2577. if (config_entry->prefix) {
  2578. strcpy(new_value, config_entry->prefix);
  2579. strcat(new_value, value);
  2580. } else
  2581. strcpy(new_value, value);
  2582. /* do the mod */
  2583. if (LDAP_CHANGETYPE_ADD == modtype) {
  2584. /* add - add to entry */
  2585. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2586. slapi_entry_attr_set_charptr(e,
  2587. types_to_generate[i],
  2588. new_value);
  2589. }
  2590. } else {
  2591. /* mod - add to mods */
  2592. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2593. slapi_mods_add_string(smods,
  2594. LDAP_MOD_REPLACE,
  2595. types_to_generate[i], new_value);
  2596. }
  2597. }
  2598. /* Make sure we don't generate for this
  2599. * type again by keeping a list of types
  2600. * we have generated for already.
  2601. */
  2602. if (LDAP_SUCCESS == ret) {
  2603. if (generated_types == NULL) {
  2604. /* If we don't have a list of generated types yet,
  2605. * we can just use the types_to_generate list so
  2606. * we don't have to allocate anything. */
  2607. generated_types = types_to_generate;
  2608. types_to_generate = NULL;
  2609. } else {
  2610. /* Just reuse the elements out of types_to_generate for the
  2611. * generated types list to avoid allocating them again. */
  2612. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  2613. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  2614. types_to_generate[i] = NULL;
  2615. }
  2616. }
  2617. }
  2618. /* free up */
  2619. slapi_ch_free_string(&value);
  2620. slapi_ch_free_string(&new_value);
  2621. slapi_ch_array_free(types_to_generate);
  2622. }
  2623. next:
  2624. list = PR_NEXT_LINK(list);
  2625. }
  2626. }
  2627. dna_unlock();
  2628. bailmod:
  2629. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  2630. /* Put the updated mods back into place. */
  2631. mods = slapi_mods_get_ldapmods_passout(smods);
  2632. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  2633. slapi_mods_free(&smods);
  2634. }
  2635. bail:
  2636. slapi_ch_array_free(generated_types);
  2637. if (free_entry && e)
  2638. slapi_entry_free(e);
  2639. if (resulting_e)
  2640. slapi_entry_free(resulting_e);
  2641. if (ret) {
  2642. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2643. "dna_pre_op: operation failure [%d]\n", ret);
  2644. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  2645. slapi_ch_free((void **)&errstr);
  2646. ret = DNA_FAILURE;
  2647. }
  2648. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2649. "<-- dna_pre_op\n");
  2650. return ret;
  2651. }
  2652. static int dna_add_pre_op(Slapi_PBlock * pb)
  2653. {
  2654. return dna_pre_op(pb, LDAP_CHANGETYPE_ADD);
  2655. }
  2656. static int dna_mod_pre_op(Slapi_PBlock * pb)
  2657. {
  2658. return dna_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  2659. }
  2660. static int dna_config_check_post_op(Slapi_PBlock * pb)
  2661. {
  2662. char *dn;
  2663. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2664. "--> dna_config_check_post_op\n");
  2665. if ((dn = dna_get_dn(pb))) {
  2666. if (dna_dn_is_config(dn))
  2667. dna_load_plugin_config();
  2668. }
  2669. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2670. "<-- dna_config_check_post_op\n");
  2671. return 0;
  2672. }
  2673. /****************************************************
  2674. * Range Extension Extended Operation
  2675. ***************************************************/
  2676. static int dna_extend_exop(Slapi_PBlock *pb)
  2677. {
  2678. int ret = -1;
  2679. struct berval *reqdata = NULL;
  2680. BerElement *tmp_bere = NULL;
  2681. char *shared_dn = NULL;
  2682. char *bind_dn = NULL;
  2683. char *oid = NULL;
  2684. PRUint64 lower = 0;
  2685. PRUint64 upper = 0;
  2686. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2687. "--> dna_extend_exop\n");
  2688. /* Fetch the request OID */
  2689. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &oid);
  2690. if (!oid) {
  2691. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2692. "dna_extend_exop: Unable to retrieve request OID.\n");
  2693. goto free_and_return;
  2694. }
  2695. /* Make sure the request OID is correct. */
  2696. if (strcmp(oid, DNA_EXTEND_EXOP_REQUEST_OID) != 0) {
  2697. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2698. "dna_extend_exop: Received incorrect request OID.\n");
  2699. goto free_and_return;
  2700. }
  2701. /* Fetch the request data */
  2702. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &reqdata);
  2703. if (!reqdata) {
  2704. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2705. "dna_extend_exop: No request data received.\n");
  2706. goto free_and_return;
  2707. }
  2708. /* decode the exop */
  2709. if ((tmp_bere = ber_init(reqdata)) == NULL) {
  2710. ret = -1;
  2711. goto free_and_return;
  2712. }
  2713. if (ber_scanf(tmp_bere, "{a}", &shared_dn) == LBER_ERROR) {
  2714. ret = -1;
  2715. goto free_and_return;
  2716. }
  2717. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2718. "dna_extend_exop: received range extension "
  2719. "request for range [%s]\n", shared_dn);
  2720. /* Only allow range requests from the replication bind DN user */
  2721. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
  2722. if (!dna_is_replica_bind_dn(shared_dn, bind_dn)) {
  2723. ret = LDAP_INSUFFICIENT_ACCESS;
  2724. goto free_and_return;
  2725. }
  2726. /* See if we have the req. range configured.
  2727. * If so, we need to see if we have range to provide. */
  2728. ret = dna_release_range(shared_dn, &lower, &upper);
  2729. if (ret == LDAP_SUCCESS) {
  2730. /* We have range to give away, so construct
  2731. * and send the response. */
  2732. BerElement *respber = NULL;
  2733. struct berval *respdata = NULL;
  2734. struct berval range_low = {0, NULL};
  2735. struct berval range_high = {0, NULL};
  2736. char lowstr[16];
  2737. char highstr[16];
  2738. /* Create the exop response */
  2739. PR_snprintf(lowstr, sizeof(lowstr), "%" NSPRIu64, lower);
  2740. PR_snprintf(highstr, sizeof(highstr), "%" NSPRIu64, upper);
  2741. range_low.bv_val = lowstr;
  2742. range_low.bv_len = strlen(range_low.bv_val);
  2743. range_high.bv_val = highstr;
  2744. range_high.bv_len = strlen(range_high.bv_val);
  2745. if ((respber = ber_alloc()) == NULL) {
  2746. ret = LDAP_NO_MEMORY;
  2747. goto free_and_return;
  2748. }
  2749. if (LBER_ERROR == (ber_printf(respber, "{oo}",
  2750. range_low.bv_val, range_low.bv_len,
  2751. range_high.bv_val, range_high.bv_len))) {
  2752. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2753. "dna_extend_exop: Unable to encode exop response.\n");
  2754. ber_free(respber, 1);
  2755. ret = LDAP_ENCODING_ERROR;
  2756. goto free_and_return;
  2757. }
  2758. ber_flatten(respber, &respdata);
  2759. ber_free(respber, 1);
  2760. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, DNA_EXTEND_EXOP_RESPONSE_OID);
  2761. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, respdata);
  2762. /* send the response ourselves */
  2763. slapi_send_ldap_result( pb, ret, NULL, NULL, 0, NULL );
  2764. ret = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  2765. ber_bvfree(respdata);
  2766. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2767. "dna_extend_exop: Released range %" NSPRIu64 "-%" NSPRIu64 ".\n",
  2768. lower, upper);
  2769. }
  2770. free_and_return:
  2771. slapi_ch_free_string(&shared_dn);
  2772. slapi_ch_free_string(&bind_dn);
  2773. if (NULL != tmp_bere) {
  2774. ber_free(tmp_bere, 1);
  2775. tmp_bere = NULL;
  2776. }
  2777. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2778. "<-- dna_extend_exop\n");
  2779. return ret;
  2780. }
  2781. /*
  2782. * dna_release_range()
  2783. *
  2784. * Checks if we have any values that we can release
  2785. * for the range specified by range_dn.
  2786. */
  2787. static int
  2788. dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper)
  2789. {
  2790. int ret = 0;
  2791. int match = 0;
  2792. PRCList *list = NULL;
  2793. Slapi_DN *cfg_base_sdn = NULL;
  2794. Slapi_DN *range_sdn = NULL;
  2795. struct configEntry *config_entry = NULL;
  2796. int set_extend_flag = 0;
  2797. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2798. "--> dna_release_range\n");
  2799. if (range_dn) {
  2800. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2801. dna_read_lock();
  2802. /* Go through the config entries to see if we
  2803. * have a shared range configured that matches
  2804. * the range from the exop request. */
  2805. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2806. list = PR_LIST_HEAD(dna_global_config);
  2807. while ((list != dna_global_config) && match != 1) {
  2808. config_entry = (struct configEntry *)list;
  2809. cfg_base_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_base);
  2810. if (slapi_sdn_compare(cfg_base_sdn, range_sdn) == 0) {
  2811. /* We found a match. Set match flag to
  2812. * break out of the loop. */
  2813. match = 1;
  2814. } else {
  2815. config_entry = NULL;
  2816. list = PR_NEXT_LINK(list);
  2817. }
  2818. slapi_sdn_free(&cfg_base_sdn);
  2819. }
  2820. }
  2821. /* config_entry will point to our match if we found one */
  2822. if (config_entry) {
  2823. int release = 0;
  2824. Slapi_PBlock *pb = NULL;
  2825. LDAPMod mod_replace;
  2826. LDAPMod *mods[2];
  2827. char *replace_val[2];
  2828. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2829. char max_value[17];
  2830. /* Need to bail if we're performing a range request
  2831. * for this range. This is to prevent the case where two
  2832. * servers are asking each other for more range for the
  2833. * same managed range. This would result in a network
  2834. * deadlock until the idletimeout kills one of the
  2835. * connections. */
  2836. slapi_lock_mutex(config_entry->extend_lock);
  2837. if (config_entry->extend_in_progress) {
  2838. /* We're already processing a range extention, so bail. */
  2839. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2840. "dna_release_range: Already processing a "
  2841. "range extension request. Skipping request.\n");
  2842. slapi_unlock_mutex(config_entry->extend_lock);
  2843. ret = LDAP_UNWILLING_TO_PERFORM;
  2844. goto bail;
  2845. } else {
  2846. /* Set a flag indicating that we're attempting to extend this range */
  2847. config_entry->extend_in_progress = 1;
  2848. set_extend_flag = 1;
  2849. slapi_unlock_mutex(config_entry->extend_lock);
  2850. }
  2851. /* Obtain the lock for this range */
  2852. slapi_lock_mutex(config_entry->lock);
  2853. /* Refuse if we're at or below our threshold */
  2854. if (config_entry->remaining <= config_entry->threshold) {
  2855. ret = LDAP_UNWILLING_TO_PERFORM;
  2856. goto bail;
  2857. }
  2858. /* If we have a next range, we need to give up values from
  2859. * it instead of from the active range */
  2860. if (config_entry->next_range_lower != 0) {
  2861. /* Release up to half of our values from the next range. */
  2862. release = (((config_entry->next_range_upper - config_entry->next_range_lower + 1) /
  2863. 2) / config_entry->threshold) * config_entry->threshold;
  2864. if (release == 0) {
  2865. ret = LDAP_UNWILLING_TO_PERFORM;
  2866. goto bail;
  2867. }
  2868. *upper = config_entry->next_range_upper;
  2869. *lower = *upper - release + 1;
  2870. /* Try to set the new next range in the config */
  2871. ret = dna_update_next_range(config_entry, config_entry->next_range_lower,
  2872. *lower - 1);
  2873. } else {
  2874. /* We release up to half of our remaining values,
  2875. * but we'll only release a range that is a multiple
  2876. * of our threshold. */
  2877. release = ((config_entry->remaining / 2) /
  2878. config_entry->threshold) * config_entry->threshold;
  2879. if (release == 0) {
  2880. ret = LDAP_UNWILLING_TO_PERFORM;
  2881. goto bail;
  2882. }
  2883. /* We give away values from the upper end of our range. */
  2884. *upper = config_entry->maxval;
  2885. *lower = *upper - release + 1;
  2886. /* try to set the new maxval in the config entry */
  2887. PR_snprintf(max_value, sizeof(max_value),"%" NSPRIu64, (*lower - 1));
  2888. /* set up our replace modify operation */
  2889. replace_val[0] = max_value;
  2890. replace_val[1] = 0;
  2891. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2892. mod_replace.mod_type = DNA_MAXVAL;
  2893. mod_replace.mod_values = replace_val;
  2894. mods[0] = &mod_replace;
  2895. mods[1] = 0;
  2896. pb = slapi_pblock_new();
  2897. if (NULL == pb) {
  2898. ret = LDAP_OPERATIONS_ERROR;
  2899. goto bail;
  2900. }
  2901. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2902. mods, 0, 0, getPluginID(), 0);
  2903. slapi_modify_internal_pb(pb);
  2904. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2905. slapi_pblock_destroy(pb);
  2906. pb = NULL;
  2907. if (ret == LDAP_SUCCESS) {
  2908. /* Adjust maxval in our cached config and shared config */
  2909. config_entry->maxval = *lower - 1;
  2910. dna_notice_allocation(config_entry, config_entry->nextval, 0, 0);
  2911. }
  2912. }
  2913. if (ret != LDAP_SUCCESS) {
  2914. /* Updating the config failed, so reset. We don't
  2915. * want to give the caller any range */
  2916. *lower = 0;
  2917. *upper = 0;
  2918. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2919. "dna_release_range: Error updating "
  2920. "configuration entry [err=%d]\n", ret);
  2921. }
  2922. }
  2923. bail:
  2924. if (set_extend_flag) {
  2925. slapi_lock_mutex(config_entry->extend_lock);
  2926. config_entry->extend_in_progress = 0;
  2927. slapi_unlock_mutex(config_entry->extend_lock);
  2928. }
  2929. if (config_entry) {
  2930. slapi_unlock_mutex(config_entry->lock);
  2931. }
  2932. slapi_sdn_free(&range_sdn);
  2933. dna_unlock();
  2934. }
  2935. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2936. "<-- dna_release_range\n");
  2937. return ret;
  2938. }
  2939. /****************************************************
  2940. End of
  2941. Functions that actually do things other
  2942. than config and startup
  2943. ****************************************************/
  2944. /**
  2945. * debug functions to print config
  2946. */
  2947. void dna_dump_config()
  2948. {
  2949. PRCList *list;
  2950. dna_read_lock();
  2951. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2952. list = PR_LIST_HEAD(dna_global_config);
  2953. while (list != dna_global_config) {
  2954. dna_dump_config_entry((struct configEntry *) list);
  2955. list = PR_NEXT_LINK(list);
  2956. }
  2957. }
  2958. dna_unlock();
  2959. }
  2960. void dna_dump_config_entry(struct configEntry * entry)
  2961. {
  2962. int i = 0;
  2963. for (i = 0; entry->types && entry->types[i]; i++) {
  2964. printf("<---- type -----------> %s\n", entry->types[i]);
  2965. }
  2966. printf("<---- filter ---------> %s\n", entry->filter);
  2967. printf("<---- prefix ---------> %s\n", entry->prefix);
  2968. printf("<---- scope ----------> %s\n", entry->scope);
  2969. printf("<---- next value -----> %" PRIu64 "\n", entry->nextval);
  2970. printf("<---- max value ------> %" PRIu64 "\n", entry->maxval);
  2971. printf("<---- interval -------> %" PRIu64 "\n", entry->interval);
  2972. printf("<---- generate flag --> %s\n", entry->generate);
  2973. printf("<---- shared cfg base > %s\n", entry->shared_cfg_base);
  2974. printf("<---- shared cfg DN --> %s\n", entry->shared_cfg_dn);
  2975. printf("<---- threshold ------> %" PRIu64 "", entry->threshold);
  2976. }