dna.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480
  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. next:
  867. list = PR_NEXT_LINK(list);
  868. if (dna_global_config == list) {
  869. /* add to tail */
  870. PR_INSERT_BEFORE(&(entry->list), list);
  871. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  872. "store [%s] at tail\n", entry->scope);
  873. entry_added = 1;
  874. break;
  875. }
  876. }
  877. } else {
  878. /* first entry */
  879. PR_INSERT_LINK(&(entry->list), dna_global_config);
  880. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  881. "store [%s] at head \n", entry->scope);
  882. entry_added = 1;
  883. }
  884. bail:
  885. if (0 == entry_added) {
  886. /* Don't log error if we weren't asked to apply config */
  887. if ((apply != 0) && (entry != NULL)) {
  888. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  889. "dna_parse_config_entry: Invalid config entry "
  890. "[%s] skipped\n", entry->dn);
  891. }
  892. dna_free_config_entry(&entry);
  893. } else {
  894. ret = DNA_SUCCESS;
  895. }
  896. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  897. "<-- dna_parse_config_entry\n");
  898. return ret;
  899. }
  900. static void
  901. dna_free_config_entry(struct configEntry ** entry)
  902. {
  903. struct configEntry *e = *entry;
  904. if (e == NULL)
  905. return;
  906. if (e->dn) {
  907. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  908. "freeing config entry [%s]\n", e->dn);
  909. slapi_ch_free_string(&e->dn);
  910. }
  911. if (e->types)
  912. slapi_ch_array_free(e->types);
  913. if (e->prefix)
  914. slapi_ch_free_string(&e->prefix);
  915. if (e->filter)
  916. slapi_ch_free_string(&e->filter);
  917. if (e->slapi_filter)
  918. slapi_filter_free(e->slapi_filter, 1);
  919. if (e->generate)
  920. slapi_ch_free_string(&e->generate);
  921. if (e->scope)
  922. slapi_ch_free_string(&e->scope);
  923. if (e->shared_cfg_base)
  924. slapi_ch_free_string(&e->shared_cfg_base);
  925. if (e->shared_cfg_dn)
  926. slapi_ch_free_string(&e->shared_cfg_dn);
  927. if (e->lock)
  928. slapi_destroy_mutex(e->lock);
  929. slapi_ch_free((void **) entry);
  930. }
  931. static void
  932. dna_delete_configEntry(PRCList *entry)
  933. {
  934. PR_REMOVE_LINK(entry);
  935. dna_free_config_entry((struct configEntry **) &entry);
  936. }
  937. static void
  938. dna_delete_config()
  939. {
  940. PRCList *list;
  941. while (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  942. list = PR_LIST_HEAD(dna_global_config);
  943. dna_delete_configEntry(list);
  944. }
  945. return;
  946. }
  947. static void
  948. dna_free_shared_server(struct dnaServer **server)
  949. {
  950. struct dnaServer *s = *server;
  951. slapi_ch_free_string(&s->host);
  952. slapi_ch_free((void **)server);
  953. }
  954. static void
  955. dna_delete_shared_servers(PRCList **servers)
  956. {
  957. PRCList *server;
  958. while (!PR_CLIST_IS_EMPTY(*servers)) {
  959. server = PR_LIST_HEAD(*servers);
  960. PR_REMOVE_LINK(server);
  961. dna_free_shared_server((struct dnaServer **)&server);
  962. }
  963. slapi_ch_free((void **)servers);
  964. *servers = NULL;
  965. return;
  966. }
  967. static int
  968. dna_load_host_port()
  969. {
  970. int status = DNA_SUCCESS;
  971. Slapi_Entry *e = NULL;
  972. Slapi_DN *config_dn = NULL;
  973. char *attrs[4];
  974. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  975. "--> dna_load_host_port\n");
  976. attrs[0] = "nsslapd-localhost";
  977. attrs[1] = "nsslapd-port";
  978. attrs[2] = "nsslapd-secureport";
  979. attrs[3] = NULL;
  980. config_dn = slapi_sdn_new_dn_byref("cn=config");
  981. if (config_dn) {
  982. slapi_search_internal_get_entry(config_dn, attrs, &e, getPluginID());
  983. slapi_sdn_free(&config_dn);
  984. }
  985. if (e) {
  986. hostname = slapi_entry_attr_get_charptr(e, "nsslapd-localhost");
  987. portnum = slapi_entry_attr_get_charptr(e, "nsslapd-port");
  988. secureportnum = slapi_entry_attr_get_charptr(e, "nsslapd-secureport");
  989. slapi_entry_free(e);
  990. }
  991. if (!hostname || !portnum) {
  992. status = DNA_FAILURE;
  993. }
  994. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  995. "<-- dna_load_host_port\n");
  996. return status;
  997. }
  998. /*
  999. * dna_update_config_event()
  1000. *
  1001. * Event queue callback that we use to do the initial
  1002. * update of the shared config entries shortly after
  1003. * startup.
  1004. */
  1005. static void
  1006. dna_update_config_event(time_t event_time, void *arg)
  1007. {
  1008. Slapi_PBlock *pb = NULL;
  1009. struct configEntry *config_entry = NULL;
  1010. PRCList *list = NULL;
  1011. /* Get read lock to prevent config changes */
  1012. dna_read_lock();
  1013. /* Loop through all config entries and update the shared
  1014. * config entries. */
  1015. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  1016. list = PR_LIST_HEAD(dna_global_config);
  1017. /* Create the pblock. We'll reuse this for all
  1018. * shared config updates. */
  1019. if ((pb = slapi_pblock_new()) == NULL)
  1020. goto bail;
  1021. while (list != dna_global_config) {
  1022. config_entry = (struct configEntry *) list;
  1023. /* If a shared config dn is set, update the shared config. */
  1024. if (config_entry->shared_cfg_dn != NULL) {
  1025. slapi_lock_mutex(config_entry->lock);
  1026. /* First delete the existing shared config entry. This
  1027. * will allow the entry to be updated for things like
  1028. * port number changes, etc. */
  1029. slapi_delete_internal_set_pb(pb, config_entry->shared_cfg_dn,
  1030. NULL, NULL, getPluginID(), 0);
  1031. /* We don't care about the results */
  1032. slapi_delete_internal_pb(pb);
  1033. /* Now force the entry to be recreated */
  1034. dna_update_shared_config(config_entry);
  1035. slapi_unlock_mutex(config_entry->lock);
  1036. slapi_pblock_init(pb);
  1037. }
  1038. list = PR_NEXT_LINK(list);
  1039. }
  1040. }
  1041. bail:
  1042. dna_unlock();
  1043. slapi_pblock_destroy(pb);
  1044. }
  1045. /****************************************************
  1046. Distributed ranges Helpers
  1047. ****************************************************/
  1048. /*
  1049. * dna_fix_maxval()
  1050. *
  1051. * Attempts to extend the range represented by
  1052. * config_entry.
  1053. *
  1054. * The lock for configEntry should be obtained
  1055. * before calling this function.
  1056. */
  1057. static int dna_fix_maxval(struct configEntry *config_entry)
  1058. {
  1059. PRCList *servers = NULL;
  1060. PRCList *server = NULL;
  1061. PRUint64 lower = 0;
  1062. PRUint64 upper = 0;
  1063. int ret = LDAP_OPERATIONS_ERROR;
  1064. if (config_entry == NULL) {
  1065. goto bail;
  1066. }
  1067. /* If we already have a next range we only need
  1068. * to activate it. */
  1069. if (config_entry->next_range_lower != 0) {
  1070. ret = dna_activate_next_range(config_entry);
  1071. if (ret != 0) {
  1072. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1073. "dna_fix_maxval: Unable to activate the "
  1074. "next range for range %s.\n", config_entry->dn);
  1075. }
  1076. } else if (config_entry->shared_cfg_base) {
  1077. /* Find out if there are any other servers to request
  1078. * range from. */
  1079. dna_get_shared_servers(config_entry, &servers);
  1080. if (servers) {
  1081. /* We have other servers we can try to extend
  1082. * our range from. Loop through them and
  1083. * request range until someone gives us some
  1084. * values, or we hit the end of the list. */
  1085. server = PR_LIST_HEAD(servers);
  1086. while (server != servers) {
  1087. if (dna_request_range(config_entry, (struct dnaServer *)server,
  1088. &lower, &upper) != 0) {
  1089. server = PR_NEXT_LINK(server);
  1090. } else {
  1091. /* Someone provided us with a new range. Attempt
  1092. * to update the config. */
  1093. if ((ret = dna_update_next_range(config_entry, lower, upper)) == 0) {
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. /* free the list of servers */
  1099. dna_delete_shared_servers(&servers);
  1100. }
  1101. }
  1102. bail:
  1103. return ret;
  1104. }
  1105. /* dna_notice_allocation()
  1106. *
  1107. * Called after a new value has been allocated from the range.
  1108. * This function will update the config entries and cached info
  1109. * appropriately. This includes activating the next range if
  1110. * we've exhausted the current range.
  1111. *
  1112. * The last parameter is the value that has just been allocated.
  1113. * The new parameter should be the next available value. If you
  1114. * set both of these parameters to 0, then this function will
  1115. * just check if the next range needs to be activated and update
  1116. * the config accordingly.
  1117. *
  1118. * The lock for configEntry should be obtained before calling
  1119. * this function. */
  1120. static void
  1121. dna_notice_allocation(struct configEntry *config_entry, PRUint64 new,
  1122. PRUint64 last, int fix)
  1123. {
  1124. /* update our cached config entry */
  1125. if ((new != 0) && (new <= (config_entry->maxval + config_entry->interval))) {
  1126. config_entry->nextval = new;
  1127. }
  1128. /* check if we've exhausted our active range */
  1129. if ((last == config_entry->maxval) ||
  1130. (config_entry->nextval > config_entry->maxval)) {
  1131. /* If we already have a next range set, make it the
  1132. * new active range. */
  1133. if (config_entry->next_range_lower != 0) {
  1134. /* Make the next range active */
  1135. if (dna_activate_next_range(config_entry) != 0) {
  1136. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1137. "dna_notice_allocation: Unable to activate "
  1138. "the next range for range %s.\n", config_entry->dn);
  1139. }
  1140. } else {
  1141. config_entry->remaining = 0;
  1142. /* update the shared configuration */
  1143. dna_update_shared_config(config_entry);
  1144. }
  1145. } else {
  1146. if (config_entry->next_range_lower != 0) {
  1147. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1148. config_entry->interval) + ((config_entry->next_range_upper -
  1149. config_entry->next_range_lower +1) / config_entry->interval);
  1150. } else {
  1151. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1152. config_entry->interval);
  1153. }
  1154. /* update the shared configuration */
  1155. dna_update_shared_config(config_entry);
  1156. }
  1157. /* Check if we passed the threshold and try to fix maxval if so. We
  1158. * don't need to do this if we already have a next range on deck. */
  1159. if ((config_entry->next_range_lower == 0) && (config_entry->remaining <= config_entry->threshold)) {
  1160. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1161. "dna_notice_allocation: Passed threshold of %" NSPRIu64 " remaining values "
  1162. "for range %s. (%" NSPRIu64 " values remain)\n",
  1163. config_entry->threshold, config_entry->dn, config_entry->remaining);
  1164. /* Only attempt to fix maxval if the fix flag is set. */
  1165. if (fix != 0) {
  1166. dna_fix_maxval(config_entry);
  1167. }
  1168. }
  1169. return;
  1170. }
  1171. static int
  1172. dna_get_shared_servers(struct configEntry *config_entry, PRCList **servers)
  1173. {
  1174. int ret = LDAP_SUCCESS;
  1175. Slapi_PBlock *pb = NULL;
  1176. Slapi_Entry **entries = NULL;
  1177. char *attrs[5];
  1178. /* First do a search in the shared config area for this
  1179. * range to find other servers who are managing this range. */
  1180. attrs[0] = DNA_HOSTNAME;
  1181. attrs[1] = DNA_PORTNUM;
  1182. attrs[2] = DNA_SECURE_PORTNUM;
  1183. attrs[3] = DNA_REMAINING;
  1184. attrs[4] = NULL;
  1185. pb = slapi_pblock_new();
  1186. if (NULL == pb) {
  1187. ret = LDAP_OPERATIONS_ERROR;
  1188. goto cleanup;
  1189. }
  1190. slapi_search_internal_set_pb(pb, config_entry->shared_cfg_base,
  1191. LDAP_SCOPE_ONELEVEL, "objectclass=*",
  1192. attrs, 0, NULL,
  1193. NULL, getPluginID(), 0);
  1194. slapi_search_internal_pb(pb);
  1195. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1196. if (LDAP_SUCCESS != ret) {
  1197. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1198. "dna_get_shared_servers: search failed for shared "
  1199. "config: %s [error %d]\n", config_entry->shared_cfg_base,
  1200. ret);
  1201. goto cleanup;
  1202. }
  1203. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1204. &entries);
  1205. if (entries && entries[0]) {
  1206. Slapi_DN *cfg_sdn = NULL;
  1207. int i;
  1208. cfg_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_dn);
  1209. /* We found some entries. Go through them and
  1210. * order them based off of remaining values. */
  1211. for (i = 0; entries[i]; i++) {
  1212. /* skip our own shared config entry */
  1213. if (slapi_sdn_compare(cfg_sdn, slapi_entry_get_sdn(entries[i]))) {
  1214. struct dnaServer *server = NULL;
  1215. /* set up the server list entry */
  1216. server = (struct dnaServer *) slapi_ch_calloc(1,
  1217. sizeof(struct dnaServer));
  1218. server->host = slapi_entry_attr_get_charptr(entries[i],
  1219. DNA_HOSTNAME);
  1220. server->port = slapi_entry_attr_get_uint(entries[i], DNA_PORTNUM);
  1221. server->secureport = slapi_entry_attr_get_uint(entries[i], DNA_SECURE_PORTNUM);
  1222. server->remaining = slapi_entry_attr_get_ulonglong(entries[i],
  1223. DNA_REMAINING);
  1224. /* validate the entry */
  1225. if (!server->host || server->port == 0 || server->remaining == 0) {
  1226. /* free and skip this one */
  1227. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1228. "dna_get_shared_servers: skipping invalid "
  1229. "shared config entry (%s)\n", slapi_entry_get_dn(entries[i]));
  1230. dna_free_shared_server(&server);
  1231. continue;
  1232. }
  1233. /* add a server entry to the list */
  1234. if (*servers == NULL) {
  1235. /* first entry */
  1236. *servers = (PRCList *) slapi_ch_calloc(1,
  1237. sizeof(struct dnaServer));
  1238. PR_INIT_CLIST(*servers);
  1239. PR_INSERT_LINK(&(server->list), *servers);
  1240. } else {
  1241. /* Find the right slot for this entry. We
  1242. * want to order the entries based off of
  1243. * the remaining number of values, higest
  1244. * to lowest. */
  1245. struct dnaServer *sitem;
  1246. PRCList* item = PR_LIST_HEAD(*servers);
  1247. while (item != *servers) {
  1248. sitem = (struct dnaServer *)item;
  1249. if (server->remaining > sitem->remaining) {
  1250. PR_INSERT_BEFORE(&(server->list), item);
  1251. break;
  1252. }
  1253. item = PR_NEXT_LINK(item);
  1254. if (*servers == item) {
  1255. /* add to tail */
  1256. PR_INSERT_BEFORE(&(server->list), item);
  1257. break;
  1258. }
  1259. }
  1260. }
  1261. }
  1262. }
  1263. slapi_sdn_free(&cfg_sdn);
  1264. }
  1265. cleanup:
  1266. slapi_free_search_results_internal(pb);
  1267. slapi_pblock_destroy(pb);
  1268. return ret;
  1269. }
  1270. /*
  1271. * dna_request_range()
  1272. *
  1273. * Requests range extension from another server.
  1274. * Returns 0 on success and will fill in upper
  1275. * and lower. Returns non-0 on failure and will
  1276. * zero out upper and lower.
  1277. */
  1278. static int dna_request_range(struct configEntry *config_entry,
  1279. struct dnaServer *server,
  1280. PRUint64 *lower, PRUint64 *upper)
  1281. {
  1282. char *bind_dn = NULL;
  1283. char *bind_passwd = NULL;
  1284. char *bind_method = NULL;
  1285. int is_ssl = 0;
  1286. struct berval *request = NULL;
  1287. char *retoid = NULL;
  1288. struct berval *responsedata = NULL;
  1289. BerElement *respber = NULL;
  1290. LDAP *ld = NULL;
  1291. char *lower_str = NULL;
  1292. char *upper_str = NULL;
  1293. int set_extend_flag = 0;
  1294. int ret = LDAP_OPERATIONS_ERROR;
  1295. int port = 0;
  1296. int timelimit;
  1297. #if defined(USE_OPENLDAP)
  1298. struct timeval timeout;
  1299. #endif
  1300. /* See if we're allowed to send a range request now */
  1301. slapi_lock_mutex(config_entry->extend_lock);
  1302. if (config_entry->extend_in_progress) {
  1303. /* We're already processing a range extention, so bail. */
  1304. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1305. "dna_request_range: Already processing a "
  1306. "range extension request. Skipping request.\n");
  1307. slapi_unlock_mutex(config_entry->extend_lock);
  1308. goto bail;
  1309. } else {
  1310. /* Set a flag indicating that we're attempting to extend this range */
  1311. config_entry->extend_in_progress = 1;
  1312. set_extend_flag = 1;
  1313. slapi_unlock_mutex(config_entry->extend_lock);
  1314. }
  1315. /* Fetch the replication bind dn info */
  1316. if (dna_get_replica_bind_creds(config_entry->shared_cfg_base, server,
  1317. &bind_dn, &bind_passwd, &bind_method,
  1318. &is_ssl, &port) != 0) {
  1319. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1320. "dna_request_range: Unable to retrieve "
  1321. "replica bind credentials.\n");
  1322. goto bail;
  1323. }
  1324. if ((request = dna_create_range_request(config_entry->shared_cfg_base)) == NULL) {
  1325. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1326. "dna_request_range: Failed to create "
  1327. "range extension extended operation request.\n");
  1328. goto bail;
  1329. }
  1330. if ((ld = slapi_ldap_init(server->host, port, is_ssl, 0)) == NULL) {
  1331. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1332. "dna_request_range: Unable to "
  1333. "initialize LDAP session to server %s:%u.\n",
  1334. server->host, server->port);
  1335. goto bail;
  1336. }
  1337. /* Disable referrals and set timelimit and a connect timeout */
  1338. ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1339. timelimit = config_entry->timeout / 1000; /* timeout is in msec */
  1340. ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &timelimit);
  1341. #if defined(USE_OPENLDAP)
  1342. timeout.tv_sec = config_entry->timeout / 1000;
  1343. timeout.tv_usec = (config_entry->timeout % 1000) * 1000;
  1344. ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &timeout);
  1345. #else
  1346. ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, &config_entry->timeout);
  1347. #endif
  1348. /* Bind to the replica server */
  1349. ret = slapi_ldap_bind(ld, bind_dn, bind_passwd, bind_method,
  1350. NULL, NULL, NULL, NULL);
  1351. if (ret != LDAP_SUCCESS) {
  1352. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1353. "dna_request_range: Error binding "
  1354. " to replica server %s:%u. [error %d]\n",
  1355. server->host, server->port, ret);
  1356. goto bail;
  1357. }
  1358. /* Send range extension request */
  1359. ret = ldap_extended_operation_s(ld, DNA_EXTEND_EXOP_REQUEST_OID,
  1360. request, NULL, NULL, &retoid, &responsedata);
  1361. if (ret != LDAP_SUCCESS) {
  1362. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1363. "dna_request_range: Error sending "
  1364. "range extension extended operation request "
  1365. "to server %s:%u [error %d]\n", server->host,
  1366. server->port, ret);
  1367. goto bail;
  1368. }
  1369. /* Verify that the OID is correct. */
  1370. if (strcmp(retoid, DNA_EXTEND_EXOP_RESPONSE_OID) != 0) {
  1371. ret = LDAP_OPERATIONS_ERROR;
  1372. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1373. "dna_request_range: Received incorrect response OID.\n");
  1374. goto bail;
  1375. }
  1376. /* Parse response */
  1377. if (responsedata) {
  1378. respber = ber_init(responsedata);
  1379. ber_scanf(respber, "{aa}", &lower_str, &upper_str);
  1380. }
  1381. /* Fill in upper and lower */
  1382. if (upper_str && lower_str) {
  1383. *upper = strtoull(upper_str, 0, 0);
  1384. *lower = strtoull(lower_str, 0, 0);
  1385. ret = 0;
  1386. } else {
  1387. ret = LDAP_OPERATIONS_ERROR;
  1388. }
  1389. bail:
  1390. if (set_extend_flag) {
  1391. slapi_lock_mutex(config_entry->extend_lock);
  1392. config_entry->extend_in_progress = 0;
  1393. slapi_unlock_mutex(config_entry->extend_lock);
  1394. }
  1395. slapi_ldap_unbind(ld);
  1396. slapi_ch_free_string(&bind_dn);
  1397. slapi_ch_free_string(&bind_passwd);
  1398. slapi_ch_free_string(&bind_method);
  1399. slapi_ch_free_string(&retoid);
  1400. slapi_ch_free_string(&lower_str);
  1401. slapi_ch_free_string(&upper_str);
  1402. ber_free(respber, 1);
  1403. ber_bvfree(request);
  1404. ber_bvfree(responsedata);
  1405. if (ret != 0) {
  1406. *upper = 0;
  1407. *lower = 0;
  1408. }
  1409. return ret;
  1410. }
  1411. static struct berval *dna_create_range_request(char *range_dn)
  1412. {
  1413. struct berval *requestdata = NULL;
  1414. struct berval shared_dn = { 0, NULL };
  1415. BerElement *ber = NULL;
  1416. shared_dn.bv_val = range_dn;
  1417. shared_dn.bv_len = strlen(shared_dn.bv_val);
  1418. if((ber = ber_alloc()) == NULL) {
  1419. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1420. "dna_create_range_request: Error "
  1421. "allocating request data.\n");
  1422. goto bail;
  1423. }
  1424. if (LBER_ERROR == (ber_printf(ber, "{o}", shared_dn.bv_val, shared_dn.bv_len))) {
  1425. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1426. "dna_create_range_request: Error "
  1427. "encoding request data.\n");
  1428. goto bail;
  1429. }
  1430. if (ber_flatten(ber, &requestdata) == -1) {
  1431. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1432. "dna_create_range_request: Error "
  1433. "encoding request data.\n");
  1434. goto bail;
  1435. }
  1436. bail:
  1437. ber_free(ber, 1);
  1438. return requestdata;
  1439. }
  1440. /****************************************************
  1441. Helpers
  1442. ****************************************************/
  1443. static char *dna_get_dn(Slapi_PBlock * pb)
  1444. {
  1445. char *dn = 0;
  1446. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1447. "--> dna_get_dn\n");
  1448. if (slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn)) {
  1449. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1450. "dna_get_dn: failed to get dn of changed entry");
  1451. goto bail;
  1452. }
  1453. bail:
  1454. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1455. "<-- dna_get_dn\n");
  1456. return dn;
  1457. }
  1458. /* config check
  1459. matching config dn or a descendent reloads config
  1460. */
  1461. static int dna_dn_is_config(char *dn)
  1462. {
  1463. int ret = 0;
  1464. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1465. "--> dna_is_config\n");
  1466. if (slapi_dn_issuffix(dn, getPluginDN())) {
  1467. ret = 1;
  1468. }
  1469. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1470. "<-- dna_is_config\n");
  1471. return ret;
  1472. }
  1473. #define DNA_LDAP_TAG_SK_REVERSE 0x81L
  1474. static LDAPControl *dna_build_sort_control(const char *attr)
  1475. {
  1476. LDAPControl *ctrl;
  1477. BerElement *ber;
  1478. int rc;
  1479. ber = ber_alloc();
  1480. if (NULL == ber)
  1481. return NULL;
  1482. rc = ber_printf(ber, "{{stb}}", attr, DNA_LDAP_TAG_SK_REVERSE, 1);
  1483. if (-1 == rc) {
  1484. ber_free(ber, 1);
  1485. return NULL;
  1486. }
  1487. rc = slapi_build_control(LDAP_CONTROL_SORTREQUEST, ber, 1, &ctrl);
  1488. ber_free(ber, 1);
  1489. if (LDAP_SUCCESS != rc)
  1490. return NULL;
  1491. return ctrl;
  1492. }
  1493. /****************************************************
  1494. Functions that actually do things other
  1495. than config and startup
  1496. ****************************************************/
  1497. /*
  1498. * dna_first_free_value()
  1499. *
  1500. * We do search all values between nextval and maxval asking the
  1501. * server to sort them, then we check the first free spot and
  1502. * use it as newval. If we go past the end of the range, we
  1503. * return LDAP_OPERATIONS_ERROR and set newval to be > the
  1504. * maximum configured value for this range. */
  1505. static int
  1506. dna_first_free_value(struct configEntry *config_entry,
  1507. PRUint64 *newval)
  1508. {
  1509. Slapi_Entry **entries = NULL;
  1510. Slapi_PBlock *pb = NULL;
  1511. LDAPControl **ctrls = NULL;
  1512. char *filter = NULL;
  1513. char *prefix;
  1514. int multitype;
  1515. int result, status, filterlen;
  1516. PRUint64 tmpval, sval, i;
  1517. char *strval = NULL;
  1518. /* check if the config is already out of range */
  1519. if (config_entry->nextval > config_entry->maxval) {
  1520. *newval = config_entry->nextval;
  1521. return LDAP_OPERATIONS_ERROR;
  1522. }
  1523. prefix = config_entry->prefix;
  1524. tmpval = config_entry->nextval;
  1525. if (dna_is_multitype_range(config_entry)) {
  1526. multitype = 1;
  1527. }
  1528. /* We don't sort if we're using a prefix (non integer type) or if this
  1529. * is a multi-type range. Instead, we just search to see if the next
  1530. * value is free, and keep incrementing until we find the next free value. */
  1531. if (prefix || multitype) {
  1532. /* This will allocate the filter string for us. */
  1533. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1534. } else {
  1535. /* This is a single-type range, so just use the first (only)
  1536. * type from the list. */
  1537. ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl));
  1538. if (NULL == ctrls)
  1539. return LDAP_OPERATIONS_ERROR;
  1540. ctrls[0] = dna_build_sort_control(config_entry->types[0]);
  1541. if (NULL == ctrls[0]) {
  1542. slapi_ch_free((void **)&ctrls);
  1543. return LDAP_OPERATIONS_ERROR;
  1544. }
  1545. filter = slapi_ch_smprintf("(&%s(&(%s>=%" NSPRIu64 ")(%s<=%" NSPRIu64 ")))",
  1546. config_entry->filter,
  1547. config_entry->types[0], tmpval,
  1548. config_entry->types[0], config_entry->maxval);
  1549. }
  1550. if (NULL == filter) {
  1551. ldap_controls_free(ctrls);
  1552. ctrls = NULL;
  1553. return LDAP_OPERATIONS_ERROR;
  1554. }
  1555. pb = slapi_pblock_new();
  1556. if (NULL == pb) {
  1557. ldap_controls_free(ctrls);
  1558. ctrls = NULL;
  1559. slapi_ch_free_string(&filter);
  1560. return LDAP_OPERATIONS_ERROR;
  1561. }
  1562. slapi_search_internal_set_pb(pb, config_entry->scope,
  1563. LDAP_SCOPE_SUBTREE, filter,
  1564. config_entry->types, 0, ctrls,
  1565. NULL, getPluginID(), 0);
  1566. slapi_search_internal_pb(pb);
  1567. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1568. if (LDAP_SUCCESS != result) {
  1569. status = LDAP_OPERATIONS_ERROR;
  1570. goto cleanup;
  1571. }
  1572. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1573. &entries);
  1574. if (NULL == entries || NULL == entries[0]) {
  1575. /* no values means we already have a good value */
  1576. *newval = tmpval;
  1577. status = LDAP_SUCCESS;
  1578. goto cleanup;
  1579. }
  1580. if (prefix || multitype) {
  1581. /* The next value identified in the config entry has already
  1582. * been taken. We just iterate through the values until we
  1583. * (hopefully) find a free one. */
  1584. for (tmpval += config_entry->interval; tmpval <= config_entry->maxval;
  1585. tmpval += config_entry->interval) {
  1586. /* This will reuse the old memory for the previous filter. It is
  1587. * guaranteed to have enough space since the filter is the same
  1588. * aside from the assertion value (we allocated enough for the
  1589. * largest supported integer). */
  1590. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1591. /* clear out the pblock so we can re-use it */
  1592. slapi_free_search_results_internal(pb);
  1593. slapi_pblock_init(pb);
  1594. slapi_search_internal_set_pb(pb, config_entry->scope,
  1595. LDAP_SCOPE_SUBTREE, filter,
  1596. config_entry->types, 0, 0,
  1597. NULL, getPluginID(), 0);
  1598. slapi_search_internal_pb(pb);
  1599. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1600. if (LDAP_SUCCESS != result) {
  1601. status = LDAP_OPERATIONS_ERROR;
  1602. goto cleanup;
  1603. }
  1604. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1605. &entries);
  1606. if (NULL == entries || NULL == entries[0]) {
  1607. /* no values means we already have a good value */
  1608. *newval = tmpval;
  1609. status = LDAP_SUCCESS;
  1610. goto cleanup;
  1611. }
  1612. }
  1613. } else {
  1614. /* Entries are sorted and filtered for value >= tval therefore if the
  1615. * first one does not match tval it means that the value is free,
  1616. * otherwise we need to cycle through values until we find a mismatch,
  1617. * the first mismatch is the first free pit. This is guaranteed to
  1618. * be a single-type range, so we can just use the first (only)
  1619. * type from the list of types directly. */
  1620. sval = 0;
  1621. for (i = 0; NULL != entries[i]; i++) {
  1622. strval = slapi_entry_attr_get_charptr(entries[i], config_entry->types[0]);
  1623. errno = 0;
  1624. sval = strtoull(strval, 0, 0);
  1625. if (errno) {
  1626. /* something very wrong here ... */
  1627. status = LDAP_OPERATIONS_ERROR;
  1628. goto cleanup;
  1629. }
  1630. slapi_ch_free_string(&strval);
  1631. if (tmpval != sval)
  1632. break;
  1633. if (config_entry->maxval < sval)
  1634. break;
  1635. tmpval += config_entry->interval;
  1636. }
  1637. }
  1638. /* check if we went past the end of the range */
  1639. if (tmpval <= config_entry->maxval) {
  1640. *newval = tmpval;
  1641. status = LDAP_SUCCESS;
  1642. } else {
  1643. /* we set newval past the end of the range
  1644. * so the caller can easily detect that we
  1645. * overflowed the configured range. */
  1646. *newval = tmpval;
  1647. status = LDAP_OPERATIONS_ERROR;
  1648. }
  1649. cleanup:
  1650. slapi_ch_free_string(&filter);
  1651. slapi_ch_free_string(&strval);
  1652. slapi_free_search_results_internal(pb);
  1653. slapi_pblock_destroy(pb);
  1654. return status;
  1655. }
  1656. /*
  1657. * Perform ldap operationally atomic increment
  1658. * Return the next value to be assigned
  1659. */
  1660. static int dna_get_next_value(struct configEntry *config_entry,
  1661. char **next_value_ret)
  1662. {
  1663. Slapi_PBlock *pb = NULL;
  1664. LDAPMod mod_replace;
  1665. LDAPMod *mods[2];
  1666. char *replace_val[2];
  1667. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1668. char next_value[17];
  1669. PRUint64 setval = 0;
  1670. PRUint64 nextval = 0;
  1671. int ret;
  1672. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1673. "--> dna_get_next_value\n");
  1674. /* get the lock to prevent contention with other threads over
  1675. * the next new value for this range. */
  1676. slapi_lock_mutex(config_entry->lock);
  1677. /* get the first value */
  1678. ret = dna_first_free_value(config_entry, &setval);
  1679. if (LDAP_SUCCESS != ret) {
  1680. /* check if we overflowed the configured range */
  1681. if (setval > config_entry->maxval) {
  1682. /* try for a new range or fail */
  1683. ret = dna_fix_maxval(config_entry);
  1684. if (LDAP_SUCCESS != ret) {
  1685. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1686. "dna_get_next_value: no more values available!!\n");
  1687. goto done;
  1688. }
  1689. /* get the first value from our newly extended range */
  1690. ret = dna_first_free_value(config_entry, &setval);
  1691. if (LDAP_SUCCESS != ret)
  1692. goto done;
  1693. } else {
  1694. /* dna_first_free_value() failed for some unknown reason */
  1695. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1696. "dna_get_next_value: failed to allocate a new ID!!\n");
  1697. goto done;
  1698. }
  1699. }
  1700. nextval = setval + config_entry->interval;
  1701. /* update nextval if we have not reached the end
  1702. * of our current range */
  1703. if ((config_entry->maxval == -1) ||
  1704. (nextval <= (config_entry->maxval + config_entry->interval))) {
  1705. /* try to set the new next value in the config entry */
  1706. PR_snprintf(next_value, sizeof(next_value),"%" NSPRIu64, nextval);
  1707. /* set up our replace modify operation */
  1708. replace_val[0] = next_value;
  1709. replace_val[1] = 0;
  1710. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1711. mod_replace.mod_type = DNA_NEXTVAL;
  1712. mod_replace.mod_values = replace_val;
  1713. mods[0] = &mod_replace;
  1714. mods[1] = 0;
  1715. pb = slapi_pblock_new();
  1716. if (NULL == pb) {
  1717. ret = LDAP_OPERATIONS_ERROR;
  1718. goto done;
  1719. }
  1720. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1721. mods, 0, 0, getPluginID(), 0);
  1722. slapi_modify_internal_pb(pb);
  1723. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1724. }
  1725. if (LDAP_SUCCESS == ret) {
  1726. slapi_ch_free_string(next_value_ret);
  1727. *next_value_ret = slapi_ch_smprintf("%" NSPRIu64, setval);
  1728. if (NULL == *next_value_ret) {
  1729. ret = LDAP_OPERATIONS_ERROR;
  1730. goto done;
  1731. }
  1732. /* update our cached config */
  1733. dna_notice_allocation(config_entry, nextval, setval, 1);
  1734. }
  1735. done:
  1736. slapi_unlock_mutex(config_entry->lock);
  1737. if (pb) {
  1738. slapi_pblock_destroy(pb);
  1739. }
  1740. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1741. "<-- dna_get_next_value\n");
  1742. return ret;
  1743. }
  1744. /*
  1745. * dna_update_shared_config()
  1746. *
  1747. * Updates the shared config entry if one is
  1748. * configured. Returns the LDAP result code.
  1749. *
  1750. * The lock for configEntry should be obtained
  1751. * before calling this function.
  1752. * */
  1753. static int
  1754. dna_update_shared_config(struct configEntry * config_entry)
  1755. {
  1756. int ret = LDAP_SUCCESS;
  1757. if (config_entry && config_entry->shared_cfg_dn) {
  1758. /* Update the shared config entry if one is configured */
  1759. Slapi_PBlock *pb = NULL;
  1760. LDAPMod mod_replace;
  1761. LDAPMod *mods[2];
  1762. char *replace_val[2];
  1763. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1764. char remaining_vals[17];
  1765. /* We store the number of remaining assigned values
  1766. * in the shared config entry. */
  1767. PR_snprintf(remaining_vals, sizeof(remaining_vals),"%" NSPRIu64, config_entry->remaining);
  1768. /* set up our replace modify operation */
  1769. replace_val[0] = remaining_vals;
  1770. replace_val[1] = 0;
  1771. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1772. mod_replace.mod_type = DNA_REMAINING;
  1773. mod_replace.mod_values = replace_val;
  1774. mods[0] = &mod_replace;
  1775. mods[1] = 0;
  1776. pb = slapi_pblock_new();
  1777. if (NULL == pb) {
  1778. ret = LDAP_OPERATIONS_ERROR;
  1779. } else {
  1780. slapi_modify_internal_set_pb(pb, config_entry->shared_cfg_dn,
  1781. mods, NULL, NULL, getPluginID(), 0);
  1782. slapi_modify_internal_pb(pb);
  1783. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1784. /* If the shared config for this instance doesn't
  1785. * already exist, we add it. */
  1786. if (ret == LDAP_NO_SUCH_OBJECT) {
  1787. Slapi_Entry *e = NULL;
  1788. /* Set up the new shared config entry */
  1789. e = slapi_entry_alloc();
  1790. /* the entry now owns the dup'd dn */
  1791. slapi_entry_init(e, slapi_ch_strdup(config_entry->shared_cfg_dn), NULL);
  1792. slapi_entry_add_string(e, SLAPI_ATTR_OBJECTCLASS, "extensibleObject");
  1793. slapi_entry_add_string(e, DNA_HOSTNAME, hostname);
  1794. slapi_entry_add_string(e, DNA_PORTNUM, portnum);
  1795. if (secureportnum) {
  1796. slapi_entry_add_string(e, DNA_SECURE_PORTNUM, secureportnum);
  1797. }
  1798. slapi_entry_add_string(e, DNA_REMAINING, remaining_vals);
  1799. /* clear pb for re-use */
  1800. slapi_pblock_init(pb);
  1801. /* e will be consumed by slapi_add_internal() */
  1802. slapi_add_entry_internal_set_pb(pb, e, NULL, getPluginID(), 0);
  1803. slapi_add_internal_pb(pb);
  1804. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1805. }
  1806. if (ret != LDAP_SUCCESS) {
  1807. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1808. "dna_update_shared_config: Unable to update shared config entry: %s [error %d]\n",
  1809. config_entry->shared_cfg_dn, ret);
  1810. }
  1811. slapi_pblock_destroy(pb);
  1812. pb = NULL;
  1813. }
  1814. }
  1815. return ret;
  1816. }
  1817. /*
  1818. * dna_update_next_range()
  1819. *
  1820. * Sets the proper value for the next range in
  1821. * all configuration entries and in-memory cache.
  1822. *
  1823. * The range you are updating should be locked
  1824. * before calling this function.
  1825. */
  1826. static int
  1827. dna_update_next_range(struct configEntry *config_entry,
  1828. PRUint64 lower, PRUint64 upper)
  1829. {
  1830. Slapi_PBlock *pb = NULL;
  1831. LDAPMod mod_replace;
  1832. LDAPMod *mods[2];
  1833. char *replace_val[2];
  1834. /* 32 for the two numbers, 1 for the '-', and one for the '\0' */
  1835. char nextrange_value[34];
  1836. int ret = 0;
  1837. /* Try to set the new next range in the config entry. */
  1838. PR_snprintf(nextrange_value, sizeof(nextrange_value), "%" NSPRIu64 "-%" NSPRIu64,
  1839. lower, upper);
  1840. /* set up our replace modify operation */
  1841. replace_val[0] = nextrange_value;
  1842. replace_val[1] = 0;
  1843. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1844. mod_replace.mod_type = DNA_NEXT_RANGE;
  1845. mod_replace.mod_values = replace_val;
  1846. mods[0] = &mod_replace;
  1847. mods[1] = 0;
  1848. pb = slapi_pblock_new();
  1849. if (NULL == pb) {
  1850. ret = LDAP_OPERATIONS_ERROR;
  1851. goto bail;
  1852. }
  1853. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1854. mods, 0, 0, getPluginID(), 0);
  1855. slapi_modify_internal_pb(pb);
  1856. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1857. slapi_pblock_destroy(pb);
  1858. pb = NULL;
  1859. if (ret != LDAP_SUCCESS) {
  1860. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1861. "dna_update_next_range: Error updating "
  1862. "configuration entry [err=%d]\n", ret);
  1863. } else {
  1864. /* update the cached config and the shared config */
  1865. config_entry->next_range_lower = lower;
  1866. config_entry->next_range_upper = upper;
  1867. dna_notice_allocation(config_entry, 0, 0, 0);
  1868. }
  1869. bail:
  1870. return ret;
  1871. }
  1872. /* dna_activate_next_range()
  1873. *
  1874. * Makes the next range the active range. This
  1875. * will update the config entry, the in-memory
  1876. * config info, and the shared config entry.
  1877. *
  1878. * The lock for configEntry should
  1879. * be obtained before calling this function.
  1880. */
  1881. static int
  1882. dna_activate_next_range(struct configEntry *config_entry)
  1883. {
  1884. Slapi_PBlock *pb = NULL;
  1885. LDAPMod mod_maxval;
  1886. LDAPMod mod_nextval;
  1887. LDAPMod mod_nextrange;
  1888. LDAPMod *mods[4];
  1889. char *maxval_vals[2];
  1890. char *nextval_vals[2];
  1891. char *nextrange_vals[1];
  1892. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1893. char maxval_val[17];
  1894. char nextval_val[17];
  1895. int ret = 0;
  1896. /* Setup the modify operation for the config entry */
  1897. PR_snprintf(maxval_val, sizeof(maxval_val),"%" NSPRIu64, config_entry->next_range_upper);
  1898. PR_snprintf(nextval_val, sizeof(nextval_val),"%" NSPRIu64, config_entry->next_range_lower);
  1899. maxval_vals[0] = maxval_val;
  1900. maxval_vals[1] = 0;
  1901. nextval_vals[0] = nextval_val;
  1902. nextval_vals[1] = 0;
  1903. nextrange_vals[0] = 0;
  1904. mod_maxval.mod_op = LDAP_MOD_REPLACE;
  1905. mod_maxval.mod_type = DNA_MAXVAL;
  1906. mod_maxval.mod_values = maxval_vals;
  1907. mod_nextval.mod_op = LDAP_MOD_REPLACE;
  1908. mod_nextval.mod_type = DNA_NEXTVAL;
  1909. mod_nextval.mod_values = nextval_vals;
  1910. mod_nextrange.mod_op = LDAP_MOD_DELETE;
  1911. mod_nextrange.mod_type = DNA_NEXT_RANGE;
  1912. mod_nextrange.mod_values = nextrange_vals;
  1913. mods[0] = &mod_maxval;
  1914. mods[1] = &mod_nextval;
  1915. mods[2] = &mod_nextrange;
  1916. mods[3] = 0;
  1917. /* Update the config entry first */
  1918. pb = slapi_pblock_new();
  1919. if (NULL == pb) {
  1920. ret = LDAP_OPERATIONS_ERROR;
  1921. goto bail;
  1922. }
  1923. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1924. mods, 0, 0, getPluginID(), 0);
  1925. slapi_modify_internal_pb(pb);
  1926. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1927. slapi_pblock_destroy(pb);
  1928. pb = NULL;
  1929. if (ret != LDAP_SUCCESS) {
  1930. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1931. "dna_activate_next_range: Error updating "
  1932. "configuration entry [err=%d]\n", ret);
  1933. } else {
  1934. /* Update the in-memory config info */
  1935. config_entry->maxval = config_entry->next_range_upper;
  1936. config_entry->nextval = config_entry->next_range_lower;
  1937. config_entry->next_range_upper = 0;
  1938. config_entry->next_range_lower = 0;
  1939. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1940. config_entry->interval);
  1941. /* update the shared configuration */
  1942. dna_update_shared_config(config_entry);
  1943. }
  1944. bail:
  1945. return ret;
  1946. }
  1947. /*
  1948. * dna_is_replica_bind_dn()
  1949. *
  1950. * Checks if the passed in DN is the replica bind DN. This
  1951. * is used to check if a user is allowed to request range
  1952. * from us.
  1953. *
  1954. * Returns 1 if bind_dn matches the replica bind dn, 0 otherwise. */
  1955. static int dna_is_replica_bind_dn(char *range_dn, char *bind_dn)
  1956. {
  1957. char *replica_dn = NULL;
  1958. Slapi_DN *replica_sdn = NULL;
  1959. char *replica_bind_dn = NULL;
  1960. Slapi_DN *replica_bind_sdn = NULL;
  1961. Slapi_DN *range_sdn = NULL;
  1962. Slapi_DN *bind_sdn = NULL;
  1963. Slapi_Entry *e = NULL;
  1964. char *attrs[2];
  1965. Slapi_Backend *be = NULL;
  1966. const char *be_suffix = NULL;
  1967. int ret = 0;
  1968. /* Find the backend suffix where the shared config is stored. */
  1969. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  1970. if ((be = slapi_be_select(range_sdn)) != NULL) {
  1971. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  1972. }
  1973. /* Fetch the "cn=replica" entry for the backend that stores
  1974. * the shared config. We need to see what the configured
  1975. * replica bind DN is. */
  1976. if (be_suffix) {
  1977. /* This function converts the old DN style to the new one. */
  1978. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  1979. if (NULL == replica_dn) {
  1980. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1981. "dna_is_replica_bind_dn: failed to create "
  1982. "replica dn for %s\n", be_suffix);
  1983. return 1;
  1984. }
  1985. replica_sdn = slapi_sdn_new_dn_passin(replica_dn);
  1986. attrs[0] = DNA_REPL_BIND_DN;
  1987. attrs[1] = 0;
  1988. /* Find cn=replica entry via search */
  1989. slapi_search_internal_get_entry(replica_sdn, attrs, &e, getPluginID());
  1990. if (e) {
  1991. replica_bind_dn = slapi_entry_attr_get_charptr(e, DNA_REPL_BIND_DN);
  1992. } else {
  1993. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1994. "dna_is_replica_bind_dn: Failed to fetch replica entry "
  1995. "for range %s\n", range_dn);
  1996. }
  1997. }
  1998. if (replica_bind_dn) {
  1999. /* Compare the passed in bind dn to the replica bind dn */
  2000. bind_sdn = slapi_sdn_new_dn_byref(bind_dn);
  2001. replica_bind_sdn = slapi_sdn_new_dn_passin(replica_bind_dn);
  2002. if (slapi_sdn_compare(bind_sdn, replica_bind_sdn) == 0) {
  2003. ret = 1;
  2004. }
  2005. }
  2006. slapi_entry_free(e);
  2007. slapi_sdn_free(&range_sdn);
  2008. slapi_sdn_free(&replica_sdn);
  2009. slapi_sdn_free(&replica_bind_sdn);
  2010. slapi_sdn_free(&bind_sdn);
  2011. return ret;
  2012. }
  2013. static int dna_get_replica_bind_creds(char *range_dn, struct dnaServer *server,
  2014. char **bind_dn, char **bind_passwd,
  2015. char **bind_method, int *is_ssl, int *port)
  2016. {
  2017. Slapi_PBlock *pb = NULL;
  2018. Slapi_DN *range_sdn = NULL;
  2019. char *replica_dn = NULL;
  2020. Slapi_Backend *be = NULL;
  2021. const char *be_suffix = NULL;
  2022. char *attrs[6];
  2023. char *filter = NULL;
  2024. char *bind_cred = NULL;
  2025. char *transport = NULL;
  2026. Slapi_Entry **entries = NULL;
  2027. int ret = LDAP_OPERATIONS_ERROR;
  2028. /* Find the backend suffix where the shared config is stored. */
  2029. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2030. if ((be = slapi_be_select(range_sdn)) != NULL) {
  2031. be_suffix = slapi_sdn_get_dn(slapi_be_getsuffix(be, 0));
  2032. }
  2033. /* Fetch the replication agreement entry */
  2034. if (be_suffix) {
  2035. /* This function converts the old DN style to the new one. */
  2036. replica_dn = slapi_create_dn_string("cn=replica,cn=\"%s\",cn=mapping tree,cn=config", be_suffix);
  2037. if (NULL == replica_dn) {
  2038. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2039. "dna_get_replica_bind_creds: failed to create "
  2040. "replica dn for %s\n", be_suffix);
  2041. ret = LDAP_PARAM_ERROR;
  2042. goto bail;
  2043. }
  2044. filter = slapi_ch_smprintf("(&(nsds5ReplicaHost=%s)(|(" DNA_REPL_PORT "=%u)"
  2045. "(" DNA_REPL_PORT "=%u)))",
  2046. server->host, server->port, server->secureport);
  2047. attrs[0] = DNA_REPL_BIND_DN;
  2048. attrs[1] = DNA_REPL_CREDS;
  2049. attrs[2] = DNA_REPL_BIND_METHOD;
  2050. attrs[3] = DNA_REPL_TRANSPORT;
  2051. attrs[4] = DNA_REPL_PORT;
  2052. attrs[5] = 0;
  2053. pb = slapi_pblock_new();
  2054. if (NULL == pb) {
  2055. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2056. "dna_get_replica_bind_creds: Failed to "
  2057. "allocate pblock\n");
  2058. goto bail;
  2059. }
  2060. slapi_search_internal_set_pb(pb, replica_dn,
  2061. LDAP_SCOPE_ONELEVEL, filter,
  2062. attrs, 0, NULL, NULL, getPluginID(), 0);
  2063. slapi_search_internal_pb(pb);
  2064. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2065. if (LDAP_SUCCESS != ret) {
  2066. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2067. "dna_get_replica_bind_creds: Failed to fetch replica "
  2068. "bind credentials for range %s, server %s, port %u [error %d]\n",
  2069. range_dn, server->host, server->port, ret);
  2070. goto bail;
  2071. }
  2072. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  2073. &entries);
  2074. if (NULL == entries || NULL == entries[0]) {
  2075. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2076. "dna_get_replica_bind_creds: Failed to fetch replication "
  2077. "agreement for range %s, server %s, port %u\n", range_dn,
  2078. server->host, server->port);
  2079. ret = LDAP_OPERATIONS_ERROR;
  2080. goto bail;
  2081. }
  2082. /* Get the replication bind dn and password from the agreement. It
  2083. * is up to the caller to free these when they are finished. */
  2084. slapi_ch_free_string(bind_dn);
  2085. slapi_ch_free_string(bind_method);
  2086. *bind_dn = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_DN);
  2087. *bind_method = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_BIND_METHOD);
  2088. bind_cred = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_CREDS);
  2089. transport = slapi_entry_attr_get_charptr(entries[0], DNA_REPL_TRANSPORT);
  2090. *port = slapi_entry_attr_get_int(entries[0], DNA_REPL_PORT);
  2091. /* Check if we should use SSL */
  2092. if (transport && (strcasecmp(transport, "SSL") == 0)) {
  2093. *is_ssl = 1;
  2094. } else if (transport && (strcasecmp(transport, "TLS") == 0)) {
  2095. *is_ssl = 2;
  2096. } else {
  2097. *is_ssl = 0;
  2098. }
  2099. /* fix up the bind method */
  2100. if ((NULL == *bind_method) || (strcasecmp(*bind_method, "SIMPLE") == 0)) {
  2101. slapi_ch_free_string(bind_method);
  2102. *bind_method = slapi_ch_strdup(LDAP_SASL_SIMPLE);
  2103. } else if (strcasecmp(*bind_method, "SSLCLIENTAUTH") == 0) {
  2104. slapi_ch_free_string(bind_method);
  2105. *bind_method = slapi_ch_strdup(LDAP_SASL_EXTERNAL);
  2106. } else if (strcasecmp(*bind_method, "SASL/GSSAPI") == 0) {
  2107. slapi_ch_free_string(bind_method);
  2108. *bind_method = slapi_ch_strdup("GSSAPI");
  2109. } else if (strcasecmp(*bind_method, "SASL/DIGEST-MD5") == 0) {
  2110. slapi_ch_free_string(bind_method);
  2111. *bind_method = slapi_ch_strdup("DIGEST-MD5");
  2112. } else { /* some other weird value */
  2113. ; /* just use it directly */
  2114. }
  2115. /* Decode the password */
  2116. if (bind_cred) {
  2117. int pw_ret = 0;
  2118. slapi_ch_free_string(bind_passwd);
  2119. pw_ret = pw_rever_decode(bind_cred, bind_passwd, DNA_REPL_CREDS);
  2120. if (pw_ret == -1) {
  2121. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2122. "dna_get_replica_bind_creds: Failed to decode "
  2123. "replica bind credentials for range %s, server %s, "
  2124. "port %u\n", range_dn, server->host, server->port);
  2125. goto bail;
  2126. } else if (pw_ret != 0) {
  2127. /* The password was already in clear text, so pw_rever_decode
  2128. * simply set bind_passwd to bind_cred. Set bind_cred to NULL
  2129. * to prevent a double free. The memory is now owned by
  2130. * bind_passwd, which is the callers responsibility to free. */
  2131. bind_cred = NULL;
  2132. }
  2133. }
  2134. }
  2135. /* If we didn't get both a bind DN and a decoded password,
  2136. * then just free everything and return an error. */
  2137. if (*bind_dn && *bind_passwd) {
  2138. ret = 0;
  2139. } else {
  2140. slapi_ch_free_string(bind_dn);
  2141. slapi_ch_free_string(bind_passwd);
  2142. slapi_ch_free_string(bind_method);
  2143. }
  2144. bail:
  2145. slapi_ch_free_string(&transport);
  2146. slapi_ch_free_string(&filter);
  2147. slapi_sdn_free(&range_sdn);
  2148. slapi_ch_free_string(&replica_dn);
  2149. slapi_ch_free_string(&bind_cred);
  2150. slapi_free_search_results_internal(pb);
  2151. slapi_pblock_destroy(pb);
  2152. return ret;
  2153. }
  2154. /*
  2155. * dna_list_contains_type()
  2156. *
  2157. * Checks if a type is contained in a list of types.
  2158. * Returns 1 if the type is found, 0 otherwise.
  2159. */
  2160. static int
  2161. dna_list_contains_type(char **list, char *type)
  2162. {
  2163. int ret = 0;
  2164. int i = 0;
  2165. if (list && type) {
  2166. for (i = 0; list[i]; i++) {
  2167. if (slapi_attr_types_equivalent(type, list[i])) {
  2168. ret = 1;
  2169. break;
  2170. }
  2171. }
  2172. }
  2173. return ret;
  2174. }
  2175. /*
  2176. * dna_list_contains_types()
  2177. *
  2178. * Checks if all types in one list (types) are contained
  2179. * in another list of types (list). Returns 1 if all
  2180. * types are found, 0 otherwise.
  2181. */
  2182. static int
  2183. dna_list_contains_types(char **list, char **types)
  2184. {
  2185. int ret = 1;
  2186. int i = 0;
  2187. int j = 0;
  2188. if (list && types) {
  2189. for (i = 0; types[i]; i++) {
  2190. int found = 0;
  2191. for (j = 0; list[j]; j++) {
  2192. if (slapi_attr_types_equivalent(types[i], list[i])) {
  2193. found = 1;
  2194. break;
  2195. }
  2196. }
  2197. if (!found) {
  2198. ret = 0;
  2199. break;
  2200. }
  2201. }
  2202. } else {
  2203. ret = 0;
  2204. }
  2205. return ret;
  2206. }
  2207. /*
  2208. * dna_list_remove_type()
  2209. *
  2210. * Removes a type from a list of types.
  2211. */
  2212. static void
  2213. dna_list_remove_type(char **list, char *type)
  2214. {
  2215. int i = 0;
  2216. int found_type = 0;
  2217. if (list && type) {
  2218. /* Go through the list until we find the type that
  2219. * we want to remove. We simply free the type we
  2220. * want to remove and shift the remaining array
  2221. * elements down by one index. This will leave us
  2222. * with two NULL elements at the end of the list,
  2223. * but this should not cause any problems. */
  2224. for (i = 0; list[i]; i++) {
  2225. if (found_type) {
  2226. list[i] = list[i + 1];
  2227. } else if (slapi_attr_types_equivalent(type, list[i])) {
  2228. slapi_ch_free_string(&list[i]);
  2229. list[i] = list[i + 1];
  2230. found_type = 1;
  2231. }
  2232. }
  2233. }
  2234. }
  2235. /*
  2236. * dna_is_multitype_range()
  2237. *
  2238. * Returns 1 if the range has multiple types configured.
  2239. * Returns 0 otherwise.
  2240. */
  2241. static int dna_is_multitype_range(struct configEntry *config_entry)
  2242. {
  2243. int ret = 0;
  2244. if (config_entry && config_entry->types && config_entry->types[1]) {
  2245. ret = 1;
  2246. }
  2247. return ret;
  2248. }
  2249. /*
  2250. * dna_create_valcheck_filter()
  2251. *
  2252. * Creates a filter string used to check if a value is free. If
  2253. * filter already holds a valid pointer, it is assumed to have enough
  2254. * space to hold the filter. This allows the same memory to be used
  2255. * over and over when you only want to change the assertion value.
  2256. * If filter contains a NULL pointer, a new string of the appropriate
  2257. * size will be allocated. The caller must free this when finished.
  2258. */
  2259. static void
  2260. dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter)
  2261. {
  2262. int filterlen = 0;
  2263. int typeslen = 0;
  2264. int i = 0;
  2265. int bytes_out = 0;
  2266. int multitype = 0;
  2267. /* Just return if we didn't get an address for the filter. */
  2268. if (filter == NULL) {
  2269. return;
  2270. }
  2271. /* To determine the filter length, we add together the following:
  2272. *
  2273. * - the string length of the filter in the config
  2274. * - the string length sum of all configured types
  2275. * - 23 bytes for each type (20 for the max string
  2276. * representation of a NSPRIu64, 3 for "(=)"
  2277. * - 3 bytes for the beginning and end of the filter - "(&" and ")"
  2278. * - 3 bytes to OR together multiple types (if present) - "(|" and ")"
  2279. * - the string length of the prefix (if one is configured) for each type
  2280. * - 1 byte for the trailing \0
  2281. *
  2282. * The filter length should be the same every time if the config struct
  2283. * has not been changed. We need to calculate the filter length even
  2284. * if we are reusing memory since we have no other way of knowing the
  2285. * length used when it was originally allocated. We trust the caller
  2286. * to only reuse the pointer with the same config struct.
  2287. */
  2288. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2289. typeslen += strlen(config_entry->types[i]);
  2290. }
  2291. if (i > 1) {
  2292. multitype = 1;
  2293. }
  2294. filterlen = strlen(config_entry->filter) + typeslen +
  2295. (i * 23) + 3 + 1 +
  2296. (config_entry->prefix ? (i * strlen(config_entry->prefix)) : 0) +
  2297. (multitype ? 3 : 0);
  2298. /* Allocate space for the filter if it hasn't been allocated yet. */
  2299. if (*filter == NULL) {
  2300. *filter = slapi_ch_malloc(filterlen);
  2301. }
  2302. /* Write out the beginning of the filter. If multiple types
  2303. * are configured, we need to OR together the search clauses
  2304. * for the types. */
  2305. if (multitype) {
  2306. bytes_out = snprintf(*filter, filterlen, "(&%s(|", config_entry->filter);
  2307. } else {
  2308. bytes_out = snprintf(*filter, filterlen, "(&%s", config_entry->filter);
  2309. }
  2310. /* Loop through the types and append each filter clause. */
  2311. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2312. bytes_out += snprintf(*filter + bytes_out, filterlen - bytes_out,
  2313. "(%s=%s%" PRIu64 ")", config_entry->types[i],
  2314. config_entry->prefix ? config_entry->prefix : "",
  2315. value);
  2316. }
  2317. /* Append the end of the filter. We need an extra paren
  2318. * to close out the OR if we have multiple types. */
  2319. if (multitype) {
  2320. strncat(*filter, "))", filterlen - bytes_out);
  2321. } else {
  2322. strncat(*filter, ")", filterlen - bytes_out);
  2323. }
  2324. }
  2325. /* for mods and adds:
  2326. where dn's are supplied, the closest in scope
  2327. is used as long as the type filter matches
  2328. and the type has not been generated yet.
  2329. */
  2330. static int dna_pre_op(Slapi_PBlock * pb, int modtype)
  2331. {
  2332. char *dn = 0;
  2333. PRCList *list = 0;
  2334. struct configEntry *config_entry = 0;
  2335. struct slapi_entry *e = 0;
  2336. Slapi_Entry *resulting_e = 0;
  2337. char *value = 0;
  2338. char **types_to_generate = NULL;
  2339. char **generated_types = NULL;
  2340. Slapi_Mods *smods = 0;
  2341. Slapi_Mod *smod = 0;
  2342. LDAPMod **mods;
  2343. int free_entry = 0;
  2344. char *errstr = NULL;
  2345. int i = 0;
  2346. int ret = 0;
  2347. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2348. "--> dna_pre_op\n");
  2349. /* Just bail if we aren't ready to service requests yet. */
  2350. if (!g_plugin_started)
  2351. goto bail;
  2352. if (0 == (dn = dna_get_dn(pb)))
  2353. goto bail;
  2354. if (LDAP_CHANGETYPE_ADD == modtype) {
  2355. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  2356. } else {
  2357. /* xxxPAR: Ideally SLAPI_MODIFY_EXISTING_ENTRY should be
  2358. * available but it turns out that is only true if you are
  2359. * a dbm backend pre-op plugin - lucky dbm backend pre-op
  2360. * plugins.
  2361. * I think that is wrong since the entry is useful for filter
  2362. * tests and schema checks and this plugin shouldn't be limited
  2363. * to a single backend type, but I don't want that fight right
  2364. * now so we go get the entry here
  2365. *
  2366. slapi_pblock_get( pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  2367. */
  2368. Slapi_DN *tmp_dn = slapi_sdn_new_dn_byref(dn);
  2369. if (tmp_dn) {
  2370. slapi_search_internal_get_entry(tmp_dn, 0, &e, getPluginID());
  2371. slapi_sdn_free(&tmp_dn);
  2372. free_entry = 1;
  2373. }
  2374. /* grab the mods - we'll put them back later with
  2375. * our modifications appended
  2376. */
  2377. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  2378. smods = slapi_mods_new();
  2379. slapi_mods_init_passin(smods, mods);
  2380. /* We need the resulting entry after the mods are applied to
  2381. * see if the entry is within the scope. */
  2382. if (e) {
  2383. resulting_e = slapi_entry_dup(e);
  2384. if (mods && (slapi_entry_apply_mods(resulting_e, mods) != LDAP_SUCCESS)) {
  2385. /* The mods don't apply cleanly, so we just let this op go
  2386. * to let the main server handle it. */
  2387. goto bailmod;
  2388. }
  2389. }
  2390. }
  2391. if (0 == e)
  2392. goto bailmod;
  2393. if (dna_dn_is_config(dn)) {
  2394. /* Validate config changes, but don't apply them.
  2395. * This allows us to reject invalid config changes
  2396. * here at the pre-op stage. Applying the config
  2397. * needs to be done at the post-op stage. */
  2398. Slapi_Entry *test_e = NULL;
  2399. /* For a MOD, we need to check the resulting entry */
  2400. if (LDAP_CHANGETYPE_ADD == modtype) {
  2401. test_e = e;
  2402. } else {
  2403. test_e = resulting_e;
  2404. }
  2405. if (dna_parse_config_entry(test_e, 0) != DNA_SUCCESS) {
  2406. /* Refuse the operation if config parsing failed. */
  2407. ret = LDAP_UNWILLING_TO_PERFORM;
  2408. if (LDAP_CHANGETYPE_ADD == modtype) {
  2409. errstr = slapi_ch_smprintf("Not a valid DNA configuration entry.");
  2410. } else {
  2411. errstr = slapi_ch_smprintf("Changes result in an invalid "
  2412. "DNA configuration.");
  2413. }
  2414. }
  2415. /* We're done, so just bail. */
  2416. goto bailmod;
  2417. }
  2418. dna_read_lock();
  2419. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2420. list = PR_LIST_HEAD(dna_global_config);
  2421. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  2422. config_entry = (struct configEntry *) list;
  2423. /* Did we already service all of these configured types? */
  2424. if (dna_list_contains_types(generated_types, config_entry->types)) {
  2425. goto next;
  2426. }
  2427. /* is the entry in scope? */
  2428. if (config_entry->scope) {
  2429. if (!slapi_dn_issuffix(dn, config_entry->scope))
  2430. goto next;
  2431. }
  2432. /* does the entry match the filter? */
  2433. if (config_entry->slapi_filter) {
  2434. Slapi_Entry *test_e = NULL;
  2435. /* For a MOD operation, we need to check the filter
  2436. * against the resulting entry. */
  2437. if (LDAP_CHANGETYPE_ADD == modtype) {
  2438. test_e = e;
  2439. } else {
  2440. test_e = resulting_e;
  2441. }
  2442. if (LDAP_SUCCESS != slapi_vattr_filter_test(pb,
  2443. test_e,
  2444. config_entry->
  2445. slapi_filter, 0))
  2446. goto next;
  2447. }
  2448. if (LDAP_CHANGETYPE_ADD == modtype) {
  2449. if (config_entry->types && dna_is_multitype_range(config_entry)) {
  2450. /* For a multi-type range, we only generate a value
  2451. * for types where the magic value is set. We do not
  2452. * generate a value for missing types. */
  2453. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2454. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  2455. if (value && !slapi_UTF8CASECMP(config_entry->generate, value)) {
  2456. slapi_ch_array_add(&types_to_generate,
  2457. slapi_ch_strdup(config_entry->types[i]));
  2458. }
  2459. slapi_ch_free_string(&value);
  2460. }
  2461. } else {
  2462. /* For a single type range, we generate the value if
  2463. * the magic value is set or if the type is missing. */
  2464. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  2465. if ((value && !slapi_UTF8CASECMP(config_entry->generate, value))
  2466. || 0 == value) {
  2467. slapi_ch_array_add(&types_to_generate,
  2468. slapi_ch_strdup(config_entry->types[0]));
  2469. }
  2470. slapi_ch_free_string(&value);
  2471. }
  2472. } else {
  2473. /* check mods for magic value */
  2474. Slapi_Mod *next_mod = slapi_mod_new();
  2475. smod = slapi_mods_get_first_smod(smods, next_mod);
  2476. while (smod) {
  2477. char *type = (char *)
  2478. slapi_mod_get_type(smod);
  2479. /* See if the type matches any configured type. */
  2480. if (dna_list_contains_type(config_entry->types, type)) {
  2481. /* If all values are being deleted, we need to
  2482. * generate a new value. We don't do this for
  2483. * multi-type ranges since they require the magic
  2484. * value to be specified to trigger generation. */
  2485. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  2486. !dna_is_multitype_range(config_entry)) {
  2487. int numvals = slapi_mod_get_num_values(smod);
  2488. if (numvals == 0) {
  2489. slapi_ch_array_add(&types_to_generate,
  2490. slapi_ch_strdup(type));
  2491. } else {
  2492. Slapi_Attr *attr = NULL;
  2493. int e_numvals = 0;
  2494. slapi_entry_attr_find(e, type, &attr);
  2495. if (attr) {
  2496. slapi_attr_get_numvalues(attr, &e_numvals);
  2497. if (numvals >= e_numvals) {
  2498. slapi_ch_array_add(&types_to_generate,
  2499. slapi_ch_strdup(type));
  2500. }
  2501. }
  2502. }
  2503. } else {
  2504. /* This is either adding or replacing a value */
  2505. struct berval *bv = slapi_mod_get_first_value(smod);
  2506. /* If this type is already in the to be generated
  2507. * list, a previous mod in this same modify operation
  2508. * either removed all values or set the magic value.
  2509. * It's possible that this mod is adding a valid value,
  2510. * which means we would not want to generate a new value.
  2511. * It is safe to remove this type from the to be
  2512. * generated list since it will be re-added here if
  2513. * necessary. */
  2514. if (dna_list_contains_type(types_to_generate, type)) {
  2515. dna_list_remove_type(types_to_generate, type);
  2516. }
  2517. /* If we have a value, see if it's the magic value. */
  2518. if (bv) {
  2519. int len = strlen(config_entry->generate);
  2520. if (len == bv->bv_len) {
  2521. if (!slapi_UTF8NCASECMP(bv->bv_val,
  2522. config_entry->generate,
  2523. len)) {
  2524. slapi_ch_array_add(&types_to_generate,
  2525. slapi_ch_strdup(type));
  2526. }
  2527. }
  2528. } else if (!dna_is_multitype_range(config_entry)) {
  2529. /* This is a replace with no new values, so we need
  2530. * to generate a new value if this is not a multi-type
  2531. * range. */
  2532. slapi_ch_array_add(&types_to_generate,
  2533. slapi_ch_strdup(type));
  2534. }
  2535. }
  2536. }
  2537. slapi_mod_done(next_mod);
  2538. smod = slapi_mods_get_next_smod(smods, next_mod);
  2539. }
  2540. slapi_mod_free(&next_mod);
  2541. }
  2542. /* We need to perform one last check for modify operations. If an
  2543. * entry within the scope has not triggered generation yet, we need
  2544. * to see if a value exists for the managed type in the resulting
  2545. * entry. This will catch a modify operation that brings an entry
  2546. * into scope for a managed range, but doesn't supply a value for
  2547. * the managed type. We don't do this for multi-type ranges. */
  2548. if ((LDAP_CHANGETYPE_MODIFY == modtype) && (!types_to_generate ||
  2549. (types_to_generate && !types_to_generate[0])) &&
  2550. !dna_is_multitype_range(config_entry)) {
  2551. Slapi_Attr *attr = NULL;
  2552. if (slapi_entry_attr_find(resulting_e, config_entry->types[0], &attr) != 0) {
  2553. slapi_ch_array_add(&types_to_generate,
  2554. slapi_ch_strdup(config_entry->types[0]));
  2555. }
  2556. }
  2557. if (types_to_generate && types_to_generate[0]) {
  2558. char *new_value;
  2559. int len;
  2560. /* create the value to add */
  2561. ret = dna_get_next_value(config_entry, &value);
  2562. if (DNA_SUCCESS != ret) {
  2563. errstr = slapi_ch_smprintf("Allocation of a new value for range"
  2564. " %s failed! Unable to proceed.",
  2565. config_entry->dn);
  2566. break;
  2567. }
  2568. len = strlen(value) + 1;
  2569. if (config_entry->prefix) {
  2570. len += strlen(config_entry->prefix);
  2571. }
  2572. new_value = slapi_ch_malloc(len);
  2573. if (config_entry->prefix) {
  2574. strcpy(new_value, config_entry->prefix);
  2575. strcat(new_value, value);
  2576. } else
  2577. strcpy(new_value, value);
  2578. /* do the mod */
  2579. if (LDAP_CHANGETYPE_ADD == modtype) {
  2580. /* add - add to entry */
  2581. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2582. slapi_entry_attr_set_charptr(e,
  2583. types_to_generate[i],
  2584. new_value);
  2585. }
  2586. } else {
  2587. /* mod - add to mods */
  2588. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2589. slapi_mods_add_string(smods,
  2590. LDAP_MOD_REPLACE,
  2591. types_to_generate[i], new_value);
  2592. }
  2593. }
  2594. /* Make sure we don't generate for this
  2595. * type again by keeping a list of types
  2596. * we have generated for already.
  2597. */
  2598. if (LDAP_SUCCESS == ret) {
  2599. if (generated_types == NULL) {
  2600. /* If we don't have a list of generated types yet,
  2601. * we can just use the types_to_generate list so
  2602. * we don't have to allocate anything. */
  2603. generated_types = types_to_generate;
  2604. types_to_generate = NULL;
  2605. } else {
  2606. /* Just reuse the elements out of types_to_generate for the
  2607. * generated types list to avoid allocating them again. */
  2608. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  2609. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  2610. types_to_generate[i] = NULL;
  2611. }
  2612. }
  2613. }
  2614. /* free up */
  2615. slapi_ch_free_string(&value);
  2616. slapi_ch_free_string(&new_value);
  2617. slapi_ch_array_free(types_to_generate);
  2618. }
  2619. next:
  2620. list = PR_NEXT_LINK(list);
  2621. }
  2622. }
  2623. dna_unlock();
  2624. bailmod:
  2625. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  2626. /* Put the updated mods back into place. */
  2627. mods = slapi_mods_get_ldapmods_passout(smods);
  2628. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  2629. slapi_mods_free(&smods);
  2630. }
  2631. bail:
  2632. slapi_ch_array_free(generated_types);
  2633. if (free_entry && e)
  2634. slapi_entry_free(e);
  2635. if (resulting_e)
  2636. slapi_entry_free(resulting_e);
  2637. if (ret) {
  2638. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2639. "dna_pre_op: operation failure [%d]\n", ret);
  2640. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  2641. slapi_ch_free((void **)&errstr);
  2642. ret = DNA_FAILURE;
  2643. }
  2644. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2645. "<-- dna_pre_op\n");
  2646. return ret;
  2647. }
  2648. static int dna_add_pre_op(Slapi_PBlock * pb)
  2649. {
  2650. return dna_pre_op(pb, LDAP_CHANGETYPE_ADD);
  2651. }
  2652. static int dna_mod_pre_op(Slapi_PBlock * pb)
  2653. {
  2654. return dna_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  2655. }
  2656. static int dna_config_check_post_op(Slapi_PBlock * pb)
  2657. {
  2658. char *dn;
  2659. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2660. "--> dna_config_check_post_op\n");
  2661. if ((dn = dna_get_dn(pb))) {
  2662. if (dna_dn_is_config(dn))
  2663. dna_load_plugin_config();
  2664. }
  2665. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2666. "<-- dna_config_check_post_op\n");
  2667. return 0;
  2668. }
  2669. /****************************************************
  2670. * Range Extension Extended Operation
  2671. ***************************************************/
  2672. static int dna_extend_exop(Slapi_PBlock *pb)
  2673. {
  2674. int ret = -1;
  2675. struct berval *reqdata = NULL;
  2676. BerElement *tmp_bere = NULL;
  2677. char *shared_dn = NULL;
  2678. char *bind_dn = NULL;
  2679. char *oid = NULL;
  2680. PRUint64 lower = 0;
  2681. PRUint64 upper = 0;
  2682. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2683. "--> dna_extend_exop\n");
  2684. /* Fetch the request OID */
  2685. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &oid);
  2686. if (!oid) {
  2687. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2688. "dna_extend_exop: Unable to retrieve request OID.\n");
  2689. goto free_and_return;
  2690. }
  2691. /* Make sure the request OID is correct. */
  2692. if (strcmp(oid, DNA_EXTEND_EXOP_REQUEST_OID) != 0) {
  2693. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2694. "dna_extend_exop: Received incorrect request OID.\n");
  2695. goto free_and_return;
  2696. }
  2697. /* Fetch the request data */
  2698. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &reqdata);
  2699. if (!reqdata) {
  2700. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2701. "dna_extend_exop: No request data received.\n");
  2702. goto free_and_return;
  2703. }
  2704. /* decode the exop */
  2705. if ((tmp_bere = ber_init(reqdata)) == NULL) {
  2706. ret = -1;
  2707. goto free_and_return;
  2708. }
  2709. if (ber_scanf(tmp_bere, "{a}", &shared_dn) == LBER_ERROR) {
  2710. ret = -1;
  2711. goto free_and_return;
  2712. }
  2713. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2714. "dna_extend_exop: received range extension "
  2715. "request for range [%s]\n", shared_dn);
  2716. /* Only allow range requests from the replication bind DN user */
  2717. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
  2718. if (!dna_is_replica_bind_dn(shared_dn, bind_dn)) {
  2719. ret = LDAP_INSUFFICIENT_ACCESS;
  2720. goto free_and_return;
  2721. }
  2722. /* See if we have the req. range configured.
  2723. * If so, we need to see if we have range to provide. */
  2724. ret = dna_release_range(shared_dn, &lower, &upper);
  2725. if (ret == LDAP_SUCCESS) {
  2726. /* We have range to give away, so construct
  2727. * and send the response. */
  2728. BerElement *respber = NULL;
  2729. struct berval *respdata = NULL;
  2730. struct berval range_low = {0, NULL};
  2731. struct berval range_high = {0, NULL};
  2732. char lowstr[16];
  2733. char highstr[16];
  2734. /* Create the exop response */
  2735. PR_snprintf(lowstr, sizeof(lowstr), "%" NSPRIu64, lower);
  2736. PR_snprintf(highstr, sizeof(highstr), "%" NSPRIu64, upper);
  2737. range_low.bv_val = lowstr;
  2738. range_low.bv_len = strlen(range_low.bv_val);
  2739. range_high.bv_val = highstr;
  2740. range_high.bv_len = strlen(range_high.bv_val);
  2741. if ((respber = ber_alloc()) == NULL) {
  2742. ret = LDAP_NO_MEMORY;
  2743. goto free_and_return;
  2744. }
  2745. if (LBER_ERROR == (ber_printf(respber, "{oo}",
  2746. range_low.bv_val, range_low.bv_len,
  2747. range_high.bv_val, range_high.bv_len))) {
  2748. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2749. "dna_extend_exop: Unable to encode exop response.\n");
  2750. ber_free(respber, 1);
  2751. ret = LDAP_ENCODING_ERROR;
  2752. goto free_and_return;
  2753. }
  2754. ber_flatten(respber, &respdata);
  2755. ber_free(respber, 1);
  2756. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, DNA_EXTEND_EXOP_RESPONSE_OID);
  2757. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, respdata);
  2758. /* send the response ourselves */
  2759. slapi_send_ldap_result( pb, ret, NULL, NULL, 0, NULL );
  2760. ret = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  2761. ber_bvfree(respdata);
  2762. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2763. "dna_extend_exop: Released range %" NSPRIu64 "-%" NSPRIu64 ".\n",
  2764. lower, upper);
  2765. }
  2766. free_and_return:
  2767. slapi_ch_free_string(&shared_dn);
  2768. slapi_ch_free_string(&bind_dn);
  2769. if (NULL != tmp_bere) {
  2770. ber_free(tmp_bere, 1);
  2771. tmp_bere = NULL;
  2772. }
  2773. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2774. "<-- dna_extend_exop\n");
  2775. return ret;
  2776. }
  2777. /*
  2778. * dna_release_range()
  2779. *
  2780. * Checks if we have any values that we can release
  2781. * for the range specified by range_dn.
  2782. */
  2783. static int
  2784. dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper)
  2785. {
  2786. int ret = 0;
  2787. int match = 0;
  2788. PRCList *list = NULL;
  2789. Slapi_DN *cfg_base_sdn = NULL;
  2790. Slapi_DN *range_sdn = NULL;
  2791. struct configEntry *config_entry = NULL;
  2792. int set_extend_flag = 0;
  2793. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2794. "--> dna_release_range\n");
  2795. if (range_dn) {
  2796. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2797. dna_read_lock();
  2798. /* Go through the config entries to see if we
  2799. * have a shared range configured that matches
  2800. * the range from the exop request. */
  2801. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2802. list = PR_LIST_HEAD(dna_global_config);
  2803. while ((list != dna_global_config) && match != 1) {
  2804. config_entry = (struct configEntry *)list;
  2805. cfg_base_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_base);
  2806. if (slapi_sdn_compare(cfg_base_sdn, range_sdn) == 0) {
  2807. /* We found a match. Set match flag to
  2808. * break out of the loop. */
  2809. match = 1;
  2810. } else {
  2811. config_entry = NULL;
  2812. list = PR_NEXT_LINK(list);
  2813. }
  2814. slapi_sdn_free(&cfg_base_sdn);
  2815. }
  2816. }
  2817. /* config_entry will point to our match if we found one */
  2818. if (config_entry) {
  2819. int release = 0;
  2820. Slapi_PBlock *pb = NULL;
  2821. LDAPMod mod_replace;
  2822. LDAPMod *mods[2];
  2823. char *replace_val[2];
  2824. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2825. char max_value[17];
  2826. /* Need to bail if we're performing a range request
  2827. * for this range. This is to prevent the case where two
  2828. * servers are asking each other for more range for the
  2829. * same managed range. This would result in a network
  2830. * deadlock until the idletimeout kills one of the
  2831. * connections. */
  2832. slapi_lock_mutex(config_entry->extend_lock);
  2833. if (config_entry->extend_in_progress) {
  2834. /* We're already processing a range extention, so bail. */
  2835. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2836. "dna_release_range: Already processing a "
  2837. "range extension request. Skipping request.\n");
  2838. slapi_unlock_mutex(config_entry->extend_lock);
  2839. ret = LDAP_UNWILLING_TO_PERFORM;
  2840. goto bail;
  2841. } else {
  2842. /* Set a flag indicating that we're attempting to extend this range */
  2843. config_entry->extend_in_progress = 1;
  2844. set_extend_flag = 1;
  2845. slapi_unlock_mutex(config_entry->extend_lock);
  2846. }
  2847. /* Obtain the lock for this range */
  2848. slapi_lock_mutex(config_entry->lock);
  2849. /* Refuse if we're at or below our threshold */
  2850. if (config_entry->remaining <= config_entry->threshold) {
  2851. ret = LDAP_UNWILLING_TO_PERFORM;
  2852. goto bail;
  2853. }
  2854. /* If we have a next range, we need to give up values from
  2855. * it instead of from the active range */
  2856. if (config_entry->next_range_lower != 0) {
  2857. /* Release up to half of our values from the next range. */
  2858. release = (((config_entry->next_range_upper - config_entry->next_range_lower + 1) /
  2859. 2) / config_entry->threshold) * config_entry->threshold;
  2860. if (release == 0) {
  2861. ret = LDAP_UNWILLING_TO_PERFORM;
  2862. goto bail;
  2863. }
  2864. *upper = config_entry->next_range_upper;
  2865. *lower = *upper - release + 1;
  2866. /* Try to set the new next range in the config */
  2867. ret = dna_update_next_range(config_entry, config_entry->next_range_lower,
  2868. *lower - 1);
  2869. } else {
  2870. /* We release up to half of our remaining values,
  2871. * but we'll only release a range that is a multiple
  2872. * of our threshold. */
  2873. release = ((config_entry->remaining / 2) /
  2874. config_entry->threshold) * config_entry->threshold;
  2875. if (release == 0) {
  2876. ret = LDAP_UNWILLING_TO_PERFORM;
  2877. goto bail;
  2878. }
  2879. /* We give away values from the upper end of our range. */
  2880. *upper = config_entry->maxval;
  2881. *lower = *upper - release + 1;
  2882. /* try to set the new maxval in the config entry */
  2883. PR_snprintf(max_value, sizeof(max_value),"%" NSPRIu64, (*lower - 1));
  2884. /* set up our replace modify operation */
  2885. replace_val[0] = max_value;
  2886. replace_val[1] = 0;
  2887. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2888. mod_replace.mod_type = DNA_MAXVAL;
  2889. mod_replace.mod_values = replace_val;
  2890. mods[0] = &mod_replace;
  2891. mods[1] = 0;
  2892. pb = slapi_pblock_new();
  2893. if (NULL == pb) {
  2894. ret = LDAP_OPERATIONS_ERROR;
  2895. goto bail;
  2896. }
  2897. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2898. mods, 0, 0, getPluginID(), 0);
  2899. slapi_modify_internal_pb(pb);
  2900. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2901. slapi_pblock_destroy(pb);
  2902. pb = NULL;
  2903. if (ret == LDAP_SUCCESS) {
  2904. /* Adjust maxval in our cached config and shared config */
  2905. config_entry->maxval = *lower - 1;
  2906. dna_notice_allocation(config_entry, config_entry->nextval, 0, 0);
  2907. }
  2908. }
  2909. if (ret != LDAP_SUCCESS) {
  2910. /* Updating the config failed, so reset. We don't
  2911. * want to give the caller any range */
  2912. *lower = 0;
  2913. *upper = 0;
  2914. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2915. "dna_release_range: Error updating "
  2916. "configuration entry [err=%d]\n", ret);
  2917. }
  2918. }
  2919. bail:
  2920. if (set_extend_flag) {
  2921. slapi_lock_mutex(config_entry->extend_lock);
  2922. config_entry->extend_in_progress = 0;
  2923. slapi_unlock_mutex(config_entry->extend_lock);
  2924. }
  2925. if (config_entry) {
  2926. slapi_unlock_mutex(config_entry->lock);
  2927. }
  2928. slapi_sdn_free(&range_sdn);
  2929. dna_unlock();
  2930. }
  2931. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2932. "<-- dna_release_range\n");
  2933. return ret;
  2934. }
  2935. /****************************************************
  2936. End of
  2937. Functions that actually do things other
  2938. than config and startup
  2939. ****************************************************/
  2940. /**
  2941. * debug functions to print config
  2942. */
  2943. void dna_dump_config()
  2944. {
  2945. PRCList *list;
  2946. dna_read_lock();
  2947. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2948. list = PR_LIST_HEAD(dna_global_config);
  2949. while (list != dna_global_config) {
  2950. dna_dump_config_entry((struct configEntry *) list);
  2951. list = PR_NEXT_LINK(list);
  2952. }
  2953. }
  2954. dna_unlock();
  2955. }
  2956. void dna_dump_config_entry(struct configEntry * entry)
  2957. {
  2958. int i = 0;
  2959. for (i = 0; entry->types && entry->types[i]; i++) {
  2960. printf("<---- type -----------> %s\n", entry->types[i]);
  2961. }
  2962. printf("<---- filter ---------> %s\n", entry->filter);
  2963. printf("<---- prefix ---------> %s\n", entry->prefix);
  2964. printf("<---- scope ----------> %s\n", entry->scope);
  2965. printf("<---- next value -----> %" PRIu64 "\n", entry->nextval);
  2966. printf("<---- max value ------> %" PRIu64 "\n", entry->maxval);
  2967. printf("<---- interval -------> %" PRIu64 "\n", entry->interval);
  2968. printf("<---- generate flag --> %s\n", entry->generate);
  2969. printf("<---- shared cfg base > %s\n", entry->shared_cfg_base);
  2970. printf("<---- shared cfg DN --> %s\n", entry->shared_cfg_dn);
  2971. printf("<---- threshold ------> %" PRIu64 "", entry->threshold);
  2972. }