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. }
  639. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  640. "----------> %s [%s]\n", DNA_PREFIX, entry->prefix);
  641. /* Set the default interval to 1 */
  642. entry->interval = 1;
  643. #ifdef DNA_ENABLE_INTERVAL
  644. value = slapi_entry_attr_get_charptr(e, DNA_INTERVAL);
  645. if (value) {
  646. entry->interval = strtoull(value, 0, 0);
  647. slapi_ch_free_string(&value);
  648. }
  649. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  650. "----------> %s [%" NSPRIu64 "]\n", DNA_INTERVAL, entry->interval);
  651. #endif
  652. value = slapi_entry_attr_get_charptr(e, DNA_GENERATE);
  653. if (value) {
  654. entry->generate = value;
  655. }
  656. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  657. "----------> %s [%s]\n", DNA_GENERATE, entry->generate);
  658. value = slapi_entry_attr_get_charptr(e, DNA_FILTER);
  659. if (value) {
  660. entry->filter = value;
  661. if (NULL == (entry->slapi_filter = slapi_str2filter(value))) {
  662. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM ,
  663. "Error: Invalid search filter in entry [%s]: [%s]\n",
  664. entry->dn, value);
  665. ret = DNA_FAILURE;
  666. goto bail;
  667. }
  668. } else {
  669. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  670. "dna_parse_config_entry: The %s config "
  671. "setting is required for range %s.\n",
  672. DNA_FILTER, entry->dn);
  673. ret = DNA_FAILURE;
  674. goto bail;
  675. }
  676. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  677. "----------> %s [%s]\n", DNA_FILTER, value);
  678. value = slapi_entry_attr_get_charptr(e, DNA_SCOPE);
  679. if (value) {
  680. /* TODO - Allow multiple scope settings for a single range. This may
  681. * make ordering the scopes tough when we put them in the clist. */
  682. entry->scope = value;
  683. } else {
  684. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  685. "dna_parse_config_entry: The %s config "
  686. "config setting is required for range %s.\n",
  687. DNA_SCOPE, entry->dn);
  688. ret = DNA_FAILURE;
  689. goto bail;
  690. }
  691. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  692. "----------> %s [%s]\n", DNA_SCOPE, entry->scope);
  693. /* optional, if not specified set -1 which is converted to the max unisgnee
  694. * value */
  695. value = slapi_entry_attr_get_charptr(e, DNA_MAXVAL);
  696. if (value) {
  697. entry->maxval = strtoull(value, 0, 0);
  698. slapi_ch_free_string(&value);
  699. } else {
  700. entry->maxval = -1;
  701. }
  702. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  703. "----------> %s [%" NSPRIu64 "]\n", DNA_MAXVAL, entry->maxval);
  704. value = slapi_entry_attr_get_charptr(e, DNA_SHARED_CFG_DN);
  705. if (value) {
  706. Slapi_Entry *shared_e = NULL;
  707. Slapi_DN *sdn = NULL;
  708. char *normdn = NULL;
  709. sdn = slapi_sdn_new_dn_byref(value);
  710. if (sdn) {
  711. slapi_search_internal_get_entry(sdn, NULL, &shared_e, getPluginID());
  712. slapi_sdn_free(&sdn);
  713. }
  714. /* Make sure that the shared config entry exists. */
  715. if (!shared_e) {
  716. /* We didn't locate the shared config container entry. Log
  717. * a message and skip this config entry. */
  718. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  719. "dna_parse_config_entry: Unable to locate "
  720. "shared configuration entry (%s)\n", value);
  721. ret = DNA_FAILURE;
  722. slapi_ch_free_string(&value);
  723. goto bail;
  724. } else {
  725. slapi_entry_free(shared_e);
  726. shared_e = NULL;
  727. }
  728. normdn = slapi_create_dn_string("%s", value);
  729. if (NULL == normdn) {
  730. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  731. "dna_parse_config_entry: failed to normalize dn: "
  732. "%s\n", value);
  733. ret = DNA_FAILURE;
  734. goto bail;
  735. }
  736. entry->shared_cfg_base = normdn;
  737. /* We prepend the host & port of this instance as a
  738. * multi-part RDN for the shared config entry. */
  739. normdn = slapi_create_dn_string("%s=%s+%s=%s,%s", DNA_HOSTNAME,
  740. hostname, DNA_PORTNUM, portnum, normdn);
  741. if (NULL == normdn) {
  742. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  743. "dna_parse_config_entry: failed to create dn: "
  744. "%s=%s+%s=%s,%s", DNA_HOSTNAME,
  745. hostname, DNA_PORTNUM, portnum, value);
  746. ret = DNA_FAILURE;
  747. goto bail;
  748. }
  749. entry->shared_cfg_dn = normdn;
  750. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  751. "----------> %s [%s]\n", DNA_SHARED_CFG_DN,
  752. entry->shared_cfg_base);
  753. }
  754. value = slapi_entry_attr_get_charptr(e, DNA_THRESHOLD);
  755. if (value) {
  756. entry->threshold = strtoull(value, 0, 0);
  757. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  758. "----------> %s [%s]\n", DNA_THRESHOLD, value);
  759. slapi_ch_free_string(&value);
  760. } else {
  761. entry->threshold = 1;
  762. }
  763. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  764. "----------> %s [%" NSPRIu64 "]\n", DNA_THRESHOLD, entry->threshold);
  765. value = slapi_entry_attr_get_charptr(e, DNA_RANGE_REQUEST_TIMEOUT);
  766. if (value) {
  767. entry->timeout = strtoull(value, 0, 0);
  768. slapi_ch_free_string(&value);
  769. } else {
  770. entry->timeout = DNA_DEFAULT_TIMEOUT;
  771. }
  772. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  773. "----------> %s [%" NSPRIu64 "]\n", DNA_RANGE_REQUEST_TIMEOUT,
  774. entry->timeout);
  775. value = slapi_entry_attr_get_charptr(e, DNA_NEXT_RANGE);
  776. if (value) {
  777. char *p = NULL;
  778. /* the next range value is in the form "<lower>-<upper>" */
  779. if ((p = strstr(value, "-")) != NULL) {
  780. *p = '\0';
  781. ++p;
  782. entry->next_range_lower = strtoull(value, 0, 0);
  783. entry->next_range_upper = strtoull(p, 0, 0);
  784. /* validate that upper is greater than lower */
  785. if (entry->next_range_upper <= entry->next_range_lower) {
  786. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  787. "dna_parse_config_entry: Illegal %s "
  788. "setting specified for range %s. Legal "
  789. "format is <lower>-<upper>.\n",
  790. DNA_NEXT_RANGE, entry->dn);
  791. ret = DNA_FAILURE;
  792. entry->next_range_lower = 0;
  793. entry->next_range_upper = 0;
  794. }
  795. /* make sure next range doesn't overlap with
  796. * the active range */
  797. if (((entry->next_range_upper <= entry->maxval) &&
  798. (entry->next_range_upper >= entry->nextval)) ||
  799. ((entry->next_range_lower <= entry->maxval) &&
  800. (entry->next_range_lower >= entry->nextval))) {
  801. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  802. "dna_parse_config_entry: Illegal %s "
  803. "setting specified for range %s. %s "
  804. "overlaps with the active range.\n",
  805. DNA_NEXT_RANGE, entry->dn, DNA_NEXT_RANGE);
  806. ret = DNA_FAILURE;
  807. entry->next_range_lower = 0;
  808. entry->next_range_upper = 0;
  809. }
  810. } else {
  811. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  812. "dna_parse_config_entry: Illegal %s "
  813. "setting specified for range %s. Legal "
  814. "format is <lower>-<upper>.\n",
  815. DNA_NEXT_RANGE, entry->dn);
  816. ret = DNA_FAILURE;
  817. }
  818. slapi_ch_free_string(&value);
  819. }
  820. /* If we were only called to validate config, we can
  821. * just bail out before applying the config changes */
  822. if (apply == 0) {
  823. goto bail;
  824. }
  825. /* Calculate number of remaining values. */
  826. if (entry->next_range_lower != 0) {
  827. entry->remaining = ((entry->next_range_upper - entry->next_range_lower + 1) /
  828. entry->interval) + ((entry->maxval - entry->nextval + 1) /
  829. entry->interval);
  830. } else if (entry->nextval >= entry->maxval) {
  831. entry->remaining = 0;
  832. } else {
  833. entry->remaining = ((entry->maxval - entry->nextval + 1) /
  834. entry->interval);
  835. }
  836. /* create the new value lock for this range */
  837. entry->lock = slapi_new_mutex();
  838. if (!entry->lock) {
  839. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  840. "dna_parse_config_entry: Unable to create lock "
  841. "for range %s.\n", entry->dn);
  842. ret = DNA_FAILURE;
  843. goto bail;
  844. }
  845. /**
  846. * Finally add the entry to the list.
  847. * We sort by scope dn length with longer
  848. * dn's first - this allows the scope
  849. * checking code to be simple and quick and
  850. * cunningly linear.
  851. */
  852. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  853. list = PR_LIST_HEAD(dna_global_config);
  854. while (list != dna_global_config) {
  855. config_entry = (struct configEntry *) list;
  856. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  857. PR_INSERT_BEFORE(&(entry->list), list);
  858. slapi_log_error(SLAPI_LOG_CONFIG,
  859. DNA_PLUGIN_SUBSYSTEM,
  860. "store [%s] before [%s] \n", entry->scope,
  861. config_entry->scope);
  862. entry_added = 1;
  863. break;
  864. }
  865. next:
  866. list = PR_NEXT_LINK(list);
  867. if (dna_global_config == list) {
  868. /* add to tail */
  869. PR_INSERT_BEFORE(&(entry->list), list);
  870. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  871. "store [%s] at tail\n", entry->scope);
  872. entry_added = 1;
  873. break;
  874. }
  875. }
  876. } else {
  877. /* first entry */
  878. PR_INSERT_LINK(&(entry->list), dna_global_config);
  879. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  880. "store [%s] at head \n", entry->scope);
  881. entry_added = 1;
  882. }
  883. bail:
  884. if (0 == entry_added) {
  885. /* Don't log error if we weren't asked to apply config */
  886. if ((apply != 0) && (entry != NULL)) {
  887. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  888. "dna_parse_config_entry: Invalid config entry "
  889. "[%s] skipped\n", entry->dn);
  890. }
  891. dna_free_config_entry(&entry);
  892. } else {
  893. ret = DNA_SUCCESS;
  894. }
  895. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  896. "<-- dna_parse_config_entry\n");
  897. return ret;
  898. }
  899. static void
  900. dna_free_config_entry(struct configEntry ** entry)
  901. {
  902. struct configEntry *e = *entry;
  903. if (e == NULL)
  904. return;
  905. if (e->dn) {
  906. slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
  907. "freeing config entry [%s]\n", e->dn);
  908. slapi_ch_free_string(&e->dn);
  909. }
  910. if (e->types)
  911. slapi_ch_array_free(e->types);
  912. if (e->prefix)
  913. slapi_ch_free_string(&e->prefix);
  914. if (e->filter)
  915. slapi_ch_free_string(&e->filter);
  916. if (e->slapi_filter)
  917. slapi_filter_free(e->slapi_filter, 1);
  918. if (e->generate)
  919. slapi_ch_free_string(&e->generate);
  920. if (e->scope)
  921. slapi_ch_free_string(&e->scope);
  922. if (e->shared_cfg_base)
  923. slapi_ch_free_string(&e->shared_cfg_base);
  924. if (e->shared_cfg_dn)
  925. slapi_ch_free_string(&e->shared_cfg_dn);
  926. if (e->lock)
  927. slapi_destroy_mutex(e->lock);
  928. slapi_ch_free((void **) entry);
  929. }
  930. static void
  931. dna_delete_configEntry(PRCList *entry)
  932. {
  933. PR_REMOVE_LINK(entry);
  934. dna_free_config_entry((struct configEntry **) &entry);
  935. }
  936. static void
  937. dna_delete_config()
  938. {
  939. PRCList *list;
  940. while (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  941. list = PR_LIST_HEAD(dna_global_config);
  942. dna_delete_configEntry(list);
  943. }
  944. return;
  945. }
  946. static void
  947. dna_free_shared_server(struct dnaServer **server)
  948. {
  949. struct dnaServer *s = *server;
  950. slapi_ch_free_string(&s->host);
  951. slapi_ch_free((void **)server);
  952. }
  953. static void
  954. dna_delete_shared_servers(PRCList **servers)
  955. {
  956. PRCList *server;
  957. while (!PR_CLIST_IS_EMPTY(*servers)) {
  958. server = PR_LIST_HEAD(*servers);
  959. PR_REMOVE_LINK(server);
  960. dna_free_shared_server((struct dnaServer **)&server);
  961. }
  962. slapi_ch_free((void **)servers);
  963. *servers = NULL;
  964. return;
  965. }
  966. static int
  967. dna_load_host_port()
  968. {
  969. int status = DNA_SUCCESS;
  970. Slapi_Entry *e = NULL;
  971. Slapi_DN *config_dn = NULL;
  972. char *attrs[4];
  973. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  974. "--> dna_load_host_port\n");
  975. attrs[0] = "nsslapd-localhost";
  976. attrs[1] = "nsslapd-port";
  977. attrs[2] = "nsslapd-secureport";
  978. attrs[3] = NULL;
  979. config_dn = slapi_sdn_new_dn_byref("cn=config");
  980. if (config_dn) {
  981. slapi_search_internal_get_entry(config_dn, attrs, &e, getPluginID());
  982. slapi_sdn_free(&config_dn);
  983. }
  984. if (e) {
  985. hostname = slapi_entry_attr_get_charptr(e, "nsslapd-localhost");
  986. portnum = slapi_entry_attr_get_charptr(e, "nsslapd-port");
  987. secureportnum = slapi_entry_attr_get_charptr(e, "nsslapd-secureport");
  988. slapi_entry_free(e);
  989. }
  990. if (!hostname || !portnum) {
  991. status = DNA_FAILURE;
  992. }
  993. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  994. "<-- dna_load_host_port\n");
  995. return status;
  996. }
  997. /*
  998. * dna_update_config_event()
  999. *
  1000. * Event queue callback that we use to do the initial
  1001. * update of the shared config entries shortly after
  1002. * startup.
  1003. */
  1004. static void
  1005. dna_update_config_event(time_t event_time, void *arg)
  1006. {
  1007. Slapi_PBlock *pb = NULL;
  1008. struct configEntry *config_entry = NULL;
  1009. PRCList *list = NULL;
  1010. /* Get read lock to prevent config changes */
  1011. dna_read_lock();
  1012. /* Loop through all config entries and update the shared
  1013. * config entries. */
  1014. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  1015. list = PR_LIST_HEAD(dna_global_config);
  1016. /* Create the pblock. We'll reuse this for all
  1017. * shared config updates. */
  1018. if ((pb = slapi_pblock_new()) == NULL)
  1019. goto bail;
  1020. while (list != dna_global_config) {
  1021. config_entry = (struct configEntry *) list;
  1022. /* If a shared config dn is set, update the shared config. */
  1023. if (config_entry->shared_cfg_dn != NULL) {
  1024. slapi_lock_mutex(config_entry->lock);
  1025. /* First delete the existing shared config entry. This
  1026. * will allow the entry to be updated for things like
  1027. * port number changes, etc. */
  1028. slapi_delete_internal_set_pb(pb, config_entry->shared_cfg_dn,
  1029. NULL, NULL, getPluginID(), 0);
  1030. /* We don't care about the results */
  1031. slapi_delete_internal_pb(pb);
  1032. /* Now force the entry to be recreated */
  1033. dna_update_shared_config(config_entry);
  1034. slapi_unlock_mutex(config_entry->lock);
  1035. slapi_pblock_init(pb);
  1036. }
  1037. list = PR_NEXT_LINK(list);
  1038. }
  1039. }
  1040. bail:
  1041. dna_unlock();
  1042. slapi_pblock_destroy(pb);
  1043. }
  1044. /****************************************************
  1045. Distributed ranges Helpers
  1046. ****************************************************/
  1047. /*
  1048. * dna_fix_maxval()
  1049. *
  1050. * Attempts to extend the range represented by
  1051. * config_entry.
  1052. *
  1053. * The lock for configEntry should be obtained
  1054. * before calling this function.
  1055. */
  1056. static int dna_fix_maxval(struct configEntry *config_entry)
  1057. {
  1058. PRCList *servers = NULL;
  1059. PRCList *server = NULL;
  1060. PRUint64 lower = 0;
  1061. PRUint64 upper = 0;
  1062. int ret = LDAP_OPERATIONS_ERROR;
  1063. if (config_entry == NULL) {
  1064. goto bail;
  1065. }
  1066. /* If we already have a next range we only need
  1067. * to activate it. */
  1068. if (config_entry->next_range_lower != 0) {
  1069. ret = dna_activate_next_range(config_entry);
  1070. if (ret != 0) {
  1071. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1072. "dna_fix_maxval: Unable to activate the "
  1073. "next range for range %s.\n", config_entry->dn);
  1074. }
  1075. } else if (config_entry->shared_cfg_base) {
  1076. /* Find out if there are any other servers to request
  1077. * range from. */
  1078. dna_get_shared_servers(config_entry, &servers);
  1079. if (servers) {
  1080. /* We have other servers we can try to extend
  1081. * our range from. Loop through them and
  1082. * request range until someone gives us some
  1083. * values, or we hit the end of the list. */
  1084. server = PR_LIST_HEAD(servers);
  1085. while (server != servers) {
  1086. if (dna_request_range(config_entry, (struct dnaServer *)server,
  1087. &lower, &upper) != 0) {
  1088. server = PR_NEXT_LINK(server);
  1089. } else {
  1090. /* Someone provided us with a new range. Attempt
  1091. * to update the config. */
  1092. if ((ret = dna_update_next_range(config_entry, lower, upper)) == 0) {
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. /* free the list of servers */
  1098. dna_delete_shared_servers(&servers);
  1099. }
  1100. }
  1101. bail:
  1102. return ret;
  1103. }
  1104. /* dna_notice_allocation()
  1105. *
  1106. * Called after a new value has been allocated from the range.
  1107. * This function will update the config entries and cached info
  1108. * appropriately. This includes activating the next range if
  1109. * we've exhausted the current range.
  1110. *
  1111. * The last parameter is the value that has just been allocated.
  1112. * The new parameter should be the next available value. If you
  1113. * set both of these parameters to 0, then this function will
  1114. * just check if the next range needs to be activated and update
  1115. * the config accordingly.
  1116. *
  1117. * The lock for configEntry should be obtained before calling
  1118. * this function. */
  1119. static void
  1120. dna_notice_allocation(struct configEntry *config_entry, PRUint64 new,
  1121. PRUint64 last, int fix)
  1122. {
  1123. /* update our cached config entry */
  1124. if ((new != 0) && (new <= (config_entry->maxval + config_entry->interval))) {
  1125. config_entry->nextval = new;
  1126. }
  1127. /* check if we've exhausted our active range */
  1128. if ((last == config_entry->maxval) ||
  1129. (config_entry->nextval > config_entry->maxval)) {
  1130. /* If we already have a next range set, make it the
  1131. * new active range. */
  1132. if (config_entry->next_range_lower != 0) {
  1133. /* Make the next range active */
  1134. if (dna_activate_next_range(config_entry) != 0) {
  1135. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1136. "dna_notice_allocation: Unable to activate "
  1137. "the next range for range %s.\n", config_entry->dn);
  1138. }
  1139. } else {
  1140. config_entry->remaining = 0;
  1141. /* update the shared configuration */
  1142. dna_update_shared_config(config_entry);
  1143. }
  1144. } else {
  1145. if (config_entry->next_range_lower != 0) {
  1146. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1147. config_entry->interval) + ((config_entry->next_range_upper -
  1148. config_entry->next_range_lower +1) / config_entry->interval);
  1149. } else {
  1150. config_entry->remaining = ((config_entry->maxval - config_entry->nextval + 1) /
  1151. config_entry->interval);
  1152. }
  1153. /* update the shared configuration */
  1154. dna_update_shared_config(config_entry);
  1155. }
  1156. /* Check if we passed the threshold and try to fix maxval if so. We
  1157. * don't need to do this if we already have a next range on deck. */
  1158. if ((config_entry->next_range_lower == 0) && (config_entry->remaining <= config_entry->threshold)) {
  1159. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1160. "dna_notice_allocation: Passed threshold of %" NSPRIu64 " remaining values "
  1161. "for range %s. (%" NSPRIu64 " values remain)\n",
  1162. config_entry->threshold, config_entry->dn, config_entry->remaining);
  1163. /* Only attempt to fix maxval if the fix flag is set. */
  1164. if (fix != 0) {
  1165. dna_fix_maxval(config_entry);
  1166. }
  1167. }
  1168. return;
  1169. }
  1170. static int
  1171. dna_get_shared_servers(struct configEntry *config_entry, PRCList **servers)
  1172. {
  1173. int ret = LDAP_SUCCESS;
  1174. Slapi_PBlock *pb = NULL;
  1175. Slapi_Entry **entries = NULL;
  1176. char *attrs[5];
  1177. /* First do a search in the shared config area for this
  1178. * range to find other servers who are managing this range. */
  1179. attrs[0] = DNA_HOSTNAME;
  1180. attrs[1] = DNA_PORTNUM;
  1181. attrs[2] = DNA_SECURE_PORTNUM;
  1182. attrs[3] = DNA_REMAINING;
  1183. attrs[4] = NULL;
  1184. pb = slapi_pblock_new();
  1185. if (NULL == pb) {
  1186. ret = LDAP_OPERATIONS_ERROR;
  1187. goto cleanup;
  1188. }
  1189. slapi_search_internal_set_pb(pb, config_entry->shared_cfg_base,
  1190. LDAP_SCOPE_ONELEVEL, "objectclass=*",
  1191. attrs, 0, NULL,
  1192. NULL, getPluginID(), 0);
  1193. slapi_search_internal_pb(pb);
  1194. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1195. if (LDAP_SUCCESS != ret) {
  1196. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1197. "dna_get_shared_servers: search failed for shared "
  1198. "config: %s [error %d]\n", config_entry->shared_cfg_base,
  1199. ret);
  1200. goto cleanup;
  1201. }
  1202. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1203. &entries);
  1204. if (entries && entries[0]) {
  1205. Slapi_DN *cfg_sdn = NULL;
  1206. int i;
  1207. cfg_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_dn);
  1208. /* We found some entries. Go through them and
  1209. * order them based off of remaining values. */
  1210. for (i = 0; entries[i]; i++) {
  1211. /* skip our own shared config entry */
  1212. if (slapi_sdn_compare(cfg_sdn, slapi_entry_get_sdn(entries[i]))) {
  1213. struct dnaServer *server = NULL;
  1214. /* set up the server list entry */
  1215. server = (struct dnaServer *) slapi_ch_calloc(1,
  1216. sizeof(struct dnaServer));
  1217. server->host = slapi_entry_attr_get_charptr(entries[i],
  1218. DNA_HOSTNAME);
  1219. server->port = slapi_entry_attr_get_uint(entries[i], DNA_PORTNUM);
  1220. server->secureport = slapi_entry_attr_get_uint(entries[i], DNA_SECURE_PORTNUM);
  1221. server->remaining = slapi_entry_attr_get_ulonglong(entries[i],
  1222. DNA_REMAINING);
  1223. /* validate the entry */
  1224. if (!server->host || server->port == 0 || server->remaining == 0) {
  1225. /* free and skip this one */
  1226. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1227. "dna_get_shared_servers: skipping invalid "
  1228. "shared config entry (%s)\n", slapi_entry_get_dn(entries[i]));
  1229. dna_free_shared_server(&server);
  1230. continue;
  1231. }
  1232. /* add a server entry to the list */
  1233. if (*servers == NULL) {
  1234. /* first entry */
  1235. *servers = (PRCList *) slapi_ch_calloc(1,
  1236. sizeof(struct dnaServer));
  1237. PR_INIT_CLIST(*servers);
  1238. PR_INSERT_LINK(&(server->list), *servers);
  1239. } else {
  1240. /* Find the right slot for this entry. We
  1241. * want to order the entries based off of
  1242. * the remaining number of values, higest
  1243. * to lowest. */
  1244. struct dnaServer *sitem;
  1245. PRCList* item = PR_LIST_HEAD(*servers);
  1246. while (item != *servers) {
  1247. sitem = (struct dnaServer *)item;
  1248. if (server->remaining > sitem->remaining) {
  1249. PR_INSERT_BEFORE(&(server->list), item);
  1250. break;
  1251. }
  1252. item = PR_NEXT_LINK(item);
  1253. if (*servers == item) {
  1254. /* add to tail */
  1255. PR_INSERT_BEFORE(&(server->list), item);
  1256. break;
  1257. }
  1258. }
  1259. }
  1260. }
  1261. }
  1262. slapi_sdn_free(&cfg_sdn);
  1263. }
  1264. cleanup:
  1265. slapi_free_search_results_internal(pb);
  1266. slapi_pblock_destroy(pb);
  1267. return ret;
  1268. }
  1269. /*
  1270. * dna_request_range()
  1271. *
  1272. * Requests range extension from another server.
  1273. * Returns 0 on success and will fill in upper
  1274. * and lower. Returns non-0 on failure and will
  1275. * zero out upper and lower.
  1276. */
  1277. static int dna_request_range(struct configEntry *config_entry,
  1278. struct dnaServer *server,
  1279. PRUint64 *lower, PRUint64 *upper)
  1280. {
  1281. char *bind_dn = NULL;
  1282. char *bind_passwd = NULL;
  1283. char *bind_method = NULL;
  1284. int is_ssl = 0;
  1285. struct berval *request = NULL;
  1286. char *retoid = NULL;
  1287. struct berval *responsedata = NULL;
  1288. BerElement *respber = NULL;
  1289. LDAP *ld = NULL;
  1290. char *lower_str = NULL;
  1291. char *upper_str = NULL;
  1292. int set_extend_flag = 0;
  1293. int ret = LDAP_OPERATIONS_ERROR;
  1294. int port = 0;
  1295. int timelimit;
  1296. #if defined(USE_OPENLDAP)
  1297. struct timeval timeout;
  1298. #endif
  1299. /* See if we're allowed to send a range request now */
  1300. slapi_lock_mutex(config_entry->extend_lock);
  1301. if (config_entry->extend_in_progress) {
  1302. /* We're already processing a range extention, so bail. */
  1303. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  1304. "dna_request_range: Already processing a "
  1305. "range extension request. Skipping request.\n");
  1306. slapi_unlock_mutex(config_entry->extend_lock);
  1307. goto bail;
  1308. } else {
  1309. /* Set a flag indicating that we're attempting to extend this range */
  1310. config_entry->extend_in_progress = 1;
  1311. set_extend_flag = 1;
  1312. slapi_unlock_mutex(config_entry->extend_lock);
  1313. }
  1314. /* Fetch the replication bind dn info */
  1315. if (dna_get_replica_bind_creds(config_entry->shared_cfg_base, server,
  1316. &bind_dn, &bind_passwd, &bind_method,
  1317. &is_ssl, &port) != 0) {
  1318. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1319. "dna_request_range: Unable to retrieve "
  1320. "replica bind credentials.\n");
  1321. goto bail;
  1322. }
  1323. if ((request = dna_create_range_request(config_entry->shared_cfg_base)) == NULL) {
  1324. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1325. "dna_request_range: Failed to create "
  1326. "range extension extended operation request.\n");
  1327. goto bail;
  1328. }
  1329. if ((ld = slapi_ldap_init(server->host, port, is_ssl, 0)) == NULL) {
  1330. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1331. "dna_request_range: Unable to "
  1332. "initialize LDAP session to server %s:%u.\n",
  1333. server->host, server->port);
  1334. goto bail;
  1335. }
  1336. /* Disable referrals and set timelimit and a connect timeout */
  1337. ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  1338. timelimit = config_entry->timeout / 1000; /* timeout is in msec */
  1339. ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &timelimit);
  1340. #if defined(USE_OPENLDAP)
  1341. timeout.tv_sec = config_entry->timeout / 1000;
  1342. timeout.tv_usec = (config_entry->timeout % 1000) * 1000;
  1343. ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &timeout);
  1344. #else
  1345. ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, &config_entry->timeout);
  1346. #endif
  1347. /* Bind to the replica server */
  1348. ret = slapi_ldap_bind(ld, bind_dn, bind_passwd, bind_method,
  1349. NULL, NULL, NULL, NULL);
  1350. if (ret != LDAP_SUCCESS) {
  1351. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1352. "dna_request_range: Error binding "
  1353. " to replica server %s:%u. [error %d]\n",
  1354. server->host, server->port, ret);
  1355. goto bail;
  1356. }
  1357. /* Send range extension request */
  1358. ret = ldap_extended_operation_s(ld, DNA_EXTEND_EXOP_REQUEST_OID,
  1359. request, NULL, NULL, &retoid, &responsedata);
  1360. if (ret != LDAP_SUCCESS) {
  1361. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1362. "dna_request_range: Error sending "
  1363. "range extension extended operation request "
  1364. "to server %s:%u [error %d]\n", server->host,
  1365. server->port, ret);
  1366. goto bail;
  1367. }
  1368. /* Verify that the OID is correct. */
  1369. if (strcmp(retoid, DNA_EXTEND_EXOP_RESPONSE_OID) != 0) {
  1370. ret = LDAP_OPERATIONS_ERROR;
  1371. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1372. "dna_request_range: Received incorrect response OID.\n");
  1373. goto bail;
  1374. }
  1375. /* Parse response */
  1376. if (responsedata) {
  1377. respber = ber_init(responsedata);
  1378. ber_scanf(respber, "{aa}", &lower_str, &upper_str);
  1379. }
  1380. /* Fill in upper and lower */
  1381. if (upper_str && lower_str) {
  1382. *upper = strtoull(upper_str, 0, 0);
  1383. *lower = strtoull(lower_str, 0, 0);
  1384. ret = 0;
  1385. } else {
  1386. ret = LDAP_OPERATIONS_ERROR;
  1387. }
  1388. bail:
  1389. if (set_extend_flag) {
  1390. slapi_lock_mutex(config_entry->extend_lock);
  1391. config_entry->extend_in_progress = 0;
  1392. slapi_unlock_mutex(config_entry->extend_lock);
  1393. }
  1394. slapi_ldap_unbind(ld);
  1395. slapi_ch_free_string(&bind_dn);
  1396. slapi_ch_free_string(&bind_passwd);
  1397. slapi_ch_free_string(&bind_method);
  1398. slapi_ch_free_string(&retoid);
  1399. slapi_ch_free_string(&lower_str);
  1400. slapi_ch_free_string(&upper_str);
  1401. ber_free(respber, 1);
  1402. ber_bvfree(request);
  1403. ber_bvfree(responsedata);
  1404. if (ret != 0) {
  1405. *upper = 0;
  1406. *lower = 0;
  1407. }
  1408. return ret;
  1409. }
  1410. static struct berval *dna_create_range_request(char *range_dn)
  1411. {
  1412. struct berval *requestdata = NULL;
  1413. struct berval shared_dn = { 0, NULL };
  1414. BerElement *ber = NULL;
  1415. shared_dn.bv_val = range_dn;
  1416. shared_dn.bv_len = strlen(shared_dn.bv_val);
  1417. if((ber = ber_alloc()) == NULL) {
  1418. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1419. "dna_create_range_request: Error "
  1420. "allocating request data.\n");
  1421. goto bail;
  1422. }
  1423. if (LBER_ERROR == (ber_printf(ber, "{o}", shared_dn.bv_val, shared_dn.bv_len))) {
  1424. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1425. "dna_create_range_request: Error "
  1426. "encoding request data.\n");
  1427. goto bail;
  1428. }
  1429. if (ber_flatten(ber, &requestdata) == -1) {
  1430. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1431. "dna_create_range_request: Error "
  1432. "encoding request data.\n");
  1433. goto bail;
  1434. }
  1435. bail:
  1436. ber_free(ber, 1);
  1437. return requestdata;
  1438. }
  1439. /****************************************************
  1440. Helpers
  1441. ****************************************************/
  1442. static char *dna_get_dn(Slapi_PBlock * pb)
  1443. {
  1444. char *dn = 0;
  1445. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1446. "--> dna_get_dn\n");
  1447. if (slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn)) {
  1448. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1449. "dna_get_dn: failed to get dn of changed entry");
  1450. goto bail;
  1451. }
  1452. bail:
  1453. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1454. "<-- dna_get_dn\n");
  1455. return dn;
  1456. }
  1457. /* config check
  1458. matching config dn or a descendent reloads config
  1459. */
  1460. static int dna_dn_is_config(char *dn)
  1461. {
  1462. int ret = 0;
  1463. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1464. "--> dna_is_config\n");
  1465. if (slapi_dn_issuffix(dn, getPluginDN())) {
  1466. ret = 1;
  1467. }
  1468. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1469. "<-- dna_is_config\n");
  1470. return ret;
  1471. }
  1472. #define DNA_LDAP_TAG_SK_REVERSE 0x81L
  1473. static LDAPControl *dna_build_sort_control(const char *attr)
  1474. {
  1475. LDAPControl *ctrl;
  1476. BerElement *ber;
  1477. int rc;
  1478. ber = ber_alloc();
  1479. if (NULL == ber)
  1480. return NULL;
  1481. rc = ber_printf(ber, "{{stb}}", attr, DNA_LDAP_TAG_SK_REVERSE, 1);
  1482. if (-1 == rc) {
  1483. ber_free(ber, 1);
  1484. return NULL;
  1485. }
  1486. rc = slapi_build_control(LDAP_CONTROL_SORTREQUEST, ber, 1, &ctrl);
  1487. ber_free(ber, 1);
  1488. if (LDAP_SUCCESS != rc)
  1489. return NULL;
  1490. return ctrl;
  1491. }
  1492. /****************************************************
  1493. Functions that actually do things other
  1494. than config and startup
  1495. ****************************************************/
  1496. /*
  1497. * dna_first_free_value()
  1498. *
  1499. * We do search all values between nextval and maxval asking the
  1500. * server to sort them, then we check the first free spot and
  1501. * use it as newval. If we go past the end of the range, we
  1502. * return LDAP_OPERATIONS_ERROR and set newval to be > the
  1503. * maximum configured value for this range. */
  1504. static int
  1505. dna_first_free_value(struct configEntry *config_entry,
  1506. PRUint64 *newval)
  1507. {
  1508. Slapi_Entry **entries = NULL;
  1509. Slapi_PBlock *pb = NULL;
  1510. LDAPControl **ctrls = NULL;
  1511. char *filter = NULL;
  1512. char *prefix;
  1513. int multitype;
  1514. int result, status, filterlen;
  1515. PRUint64 tmpval, sval, i;
  1516. char *strval = NULL;
  1517. /* check if the config is already out of range */
  1518. if (config_entry->nextval > config_entry->maxval) {
  1519. *newval = config_entry->nextval;
  1520. return LDAP_OPERATIONS_ERROR;
  1521. }
  1522. prefix = config_entry->prefix;
  1523. tmpval = config_entry->nextval;
  1524. if (dna_is_multitype_range(config_entry)) {
  1525. multitype = 1;
  1526. }
  1527. /* We don't sort if we're using a prefix (non integer type) or if this
  1528. * is a multi-type range. Instead, we just search to see if the next
  1529. * value is free, and keep incrementing until we find the next free value. */
  1530. if (prefix || multitype) {
  1531. /* This will allocate the filter string for us. */
  1532. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1533. } else {
  1534. /* This is a single-type range, so just use the first (only)
  1535. * type from the list. */
  1536. ctrls = (LDAPControl **)slapi_ch_calloc(2, sizeof(LDAPControl));
  1537. if (NULL == ctrls)
  1538. return LDAP_OPERATIONS_ERROR;
  1539. ctrls[0] = dna_build_sort_control(config_entry->types[0]);
  1540. if (NULL == ctrls[0]) {
  1541. slapi_ch_free((void **)&ctrls);
  1542. return LDAP_OPERATIONS_ERROR;
  1543. }
  1544. filter = slapi_ch_smprintf("(&%s(&(%s>=%" NSPRIu64 ")(%s<=%" NSPRIu64 ")))",
  1545. config_entry->filter,
  1546. config_entry->types[0], tmpval,
  1547. config_entry->types[0], config_entry->maxval);
  1548. }
  1549. if (NULL == filter) {
  1550. ldap_controls_free(ctrls);
  1551. ctrls = NULL;
  1552. return LDAP_OPERATIONS_ERROR;
  1553. }
  1554. pb = slapi_pblock_new();
  1555. if (NULL == pb) {
  1556. ldap_controls_free(ctrls);
  1557. ctrls = NULL;
  1558. slapi_ch_free_string(&filter);
  1559. return LDAP_OPERATIONS_ERROR;
  1560. }
  1561. slapi_search_internal_set_pb(pb, config_entry->scope,
  1562. LDAP_SCOPE_SUBTREE, filter,
  1563. config_entry->types, 0, ctrls,
  1564. NULL, getPluginID(), 0);
  1565. slapi_search_internal_pb(pb);
  1566. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1567. if (LDAP_SUCCESS != result) {
  1568. status = LDAP_OPERATIONS_ERROR;
  1569. goto cleanup;
  1570. }
  1571. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1572. &entries);
  1573. if (NULL == entries || NULL == entries[0]) {
  1574. /* no values means we already have a good value */
  1575. *newval = tmpval;
  1576. status = LDAP_SUCCESS;
  1577. goto cleanup;
  1578. }
  1579. if (prefix || multitype) {
  1580. /* The next value identified in the config entry has already
  1581. * been taken. We just iterate through the values until we
  1582. * (hopefully) find a free one. */
  1583. for (tmpval += config_entry->interval; tmpval <= config_entry->maxval;
  1584. tmpval += config_entry->interval) {
  1585. /* This will reuse the old memory for the previous filter. It is
  1586. * guaranteed to have enough space since the filter is the same
  1587. * aside from the assertion value (we allocated enough for the
  1588. * largest supported integer). */
  1589. dna_create_valcheck_filter(config_entry, tmpval, &filter);
  1590. /* clear out the pblock so we can re-use it */
  1591. slapi_free_search_results_internal(pb);
  1592. slapi_pblock_init(pb);
  1593. slapi_search_internal_set_pb(pb, config_entry->scope,
  1594. LDAP_SCOPE_SUBTREE, filter,
  1595. config_entry->types, 0, 0,
  1596. NULL, getPluginID(), 0);
  1597. slapi_search_internal_pb(pb);
  1598. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1599. if (LDAP_SUCCESS != result) {
  1600. status = LDAP_OPERATIONS_ERROR;
  1601. goto cleanup;
  1602. }
  1603. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1604. &entries);
  1605. if (NULL == entries || NULL == entries[0]) {
  1606. /* no values means we already have a good value */
  1607. *newval = tmpval;
  1608. status = LDAP_SUCCESS;
  1609. goto cleanup;
  1610. }
  1611. }
  1612. } else {
  1613. /* Entries are sorted and filtered for value >= tval therefore if the
  1614. * first one does not match tval it means that the value is free,
  1615. * otherwise we need to cycle through values until we find a mismatch,
  1616. * the first mismatch is the first free pit. This is guaranteed to
  1617. * be a single-type range, so we can just use the first (only)
  1618. * type from the list of types directly. */
  1619. sval = 0;
  1620. for (i = 0; NULL != entries[i]; i++) {
  1621. strval = slapi_entry_attr_get_charptr(entries[i], config_entry->types[0]);
  1622. errno = 0;
  1623. sval = strtoull(strval, 0, 0);
  1624. if (errno) {
  1625. /* something very wrong here ... */
  1626. status = LDAP_OPERATIONS_ERROR;
  1627. goto cleanup;
  1628. }
  1629. slapi_ch_free_string(&strval);
  1630. if (tmpval != sval)
  1631. break;
  1632. if (config_entry->maxval < sval)
  1633. break;
  1634. tmpval += config_entry->interval;
  1635. }
  1636. }
  1637. /* check if we went past the end of the range */
  1638. if (tmpval <= config_entry->maxval) {
  1639. *newval = tmpval;
  1640. status = LDAP_SUCCESS;
  1641. } else {
  1642. /* we set newval past the end of the range
  1643. * so the caller can easily detect that we
  1644. * overflowed the configured range. */
  1645. *newval = tmpval;
  1646. status = LDAP_OPERATIONS_ERROR;
  1647. }
  1648. cleanup:
  1649. slapi_ch_free_string(&filter);
  1650. slapi_ch_free_string(&strval);
  1651. slapi_free_search_results_internal(pb);
  1652. slapi_pblock_destroy(pb);
  1653. return status;
  1654. }
  1655. /*
  1656. * Perform ldap operationally atomic increment
  1657. * Return the next value to be assigned
  1658. */
  1659. static int dna_get_next_value(struct configEntry *config_entry,
  1660. char **next_value_ret)
  1661. {
  1662. Slapi_PBlock *pb = NULL;
  1663. LDAPMod mod_replace;
  1664. LDAPMod *mods[2];
  1665. char *replace_val[2];
  1666. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  1667. char next_value[17];
  1668. PRUint64 setval = 0;
  1669. PRUint64 nextval = 0;
  1670. int ret;
  1671. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  1672. "--> dna_get_next_value\n");
  1673. /* get the lock to prevent contention with other threads over
  1674. * the next new value for this range. */
  1675. slapi_lock_mutex(config_entry->lock);
  1676. /* get the first value */
  1677. ret = dna_first_free_value(config_entry, &setval);
  1678. if (LDAP_SUCCESS != ret) {
  1679. /* check if we overflowed the configured range */
  1680. if (setval > config_entry->maxval) {
  1681. /* try for a new range or fail */
  1682. ret = dna_fix_maxval(config_entry);
  1683. if (LDAP_SUCCESS != ret) {
  1684. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1685. "dna_get_next_value: no more values available!!\n");
  1686. goto done;
  1687. }
  1688. /* get the first value from our newly extended range */
  1689. ret = dna_first_free_value(config_entry, &setval);
  1690. if (LDAP_SUCCESS != ret)
  1691. goto done;
  1692. } else {
  1693. /* dna_first_free_value() failed for some unknown reason */
  1694. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  1695. "dna_get_next_value: failed to allocate a new ID!!\n");
  1696. goto done;
  1697. }
  1698. }
  1699. nextval = setval + config_entry->interval;
  1700. /* update nextval if we have not reached the end
  1701. * of our current range */
  1702. if ((config_entry->maxval == -1) ||
  1703. (nextval <= (config_entry->maxval + config_entry->interval))) {
  1704. /* try to set the new next value in the config entry */
  1705. PR_snprintf(next_value, sizeof(next_value),"%" NSPRIu64, nextval);
  1706. /* set up our replace modify operation */
  1707. replace_val[0] = next_value;
  1708. replace_val[1] = 0;
  1709. mod_replace.mod_op = LDAP_MOD_REPLACE;
  1710. mod_replace.mod_type = DNA_NEXTVAL;
  1711. mod_replace.mod_values = replace_val;
  1712. mods[0] = &mod_replace;
  1713. mods[1] = 0;
  1714. pb = slapi_pblock_new();
  1715. if (NULL == pb) {
  1716. ret = LDAP_OPERATIONS_ERROR;
  1717. goto done;
  1718. }
  1719. slapi_modify_internal_set_pb(pb, config_entry->dn,
  1720. mods, 0, 0, getPluginID(), 0);
  1721. slapi_modify_internal_pb(pb);
  1722. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1723. slapi_pblock_destroy(pb);
  1724. pb = NULL;
  1725. }
  1726. if (LDAP_SUCCESS == ret) {
  1727. slapi_ch_free_string(next_value_ret);
  1728. *next_value_ret = slapi_ch_smprintf("%" NSPRIu64, setval);
  1729. if (NULL == *next_value_ret) {
  1730. ret = LDAP_OPERATIONS_ERROR;
  1731. goto done;
  1732. }
  1733. /* update our cached config */
  1734. dna_notice_allocation(config_entry, nextval, setval, 1);
  1735. }
  1736. done:
  1737. slapi_unlock_mutex(config_entry->lock);
  1738. if (pb)
  1739. slapi_pblock_destroy(pb);
  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(&filter);
  2146. slapi_sdn_free(&range_sdn);
  2147. slapi_ch_free_string(&replica_dn);
  2148. slapi_ch_free_string(&bind_cred);
  2149. slapi_free_search_results_internal(pb);
  2150. slapi_pblock_destroy(pb);
  2151. return ret;
  2152. }
  2153. /*
  2154. * dna_list_contains_type()
  2155. *
  2156. * Checks if a type is contained in a list of types.
  2157. * Returns 1 if the type is found, 0 otherwise.
  2158. */
  2159. static int
  2160. dna_list_contains_type(char **list, char *type)
  2161. {
  2162. int ret = 0;
  2163. int i = 0;
  2164. if (list && type) {
  2165. for (i = 0; list[i]; i++) {
  2166. if (slapi_attr_types_equivalent(type, list[i])) {
  2167. ret = 1;
  2168. break;
  2169. }
  2170. }
  2171. }
  2172. return ret;
  2173. }
  2174. /*
  2175. * dna_list_contains_types()
  2176. *
  2177. * Checks if all types in one list (types) are contained
  2178. * in another list of types (list). Returns 1 if all
  2179. * types are found, 0 otherwise.
  2180. */
  2181. static int
  2182. dna_list_contains_types(char **list, char **types)
  2183. {
  2184. int ret = 1;
  2185. int i = 0;
  2186. int j = 0;
  2187. if (list && types) {
  2188. for (i = 0; types[i]; i++) {
  2189. int found = 0;
  2190. for (j = 0; list[j]; j++) {
  2191. if (slapi_attr_types_equivalent(types[i], list[i])) {
  2192. found = 1;
  2193. break;
  2194. }
  2195. }
  2196. if (!found) {
  2197. ret = 0;
  2198. break;
  2199. }
  2200. }
  2201. } else {
  2202. ret = 0;
  2203. }
  2204. return ret;
  2205. }
  2206. /*
  2207. * dna_list_remove_type()
  2208. *
  2209. * Removes a type from a list of types.
  2210. */
  2211. static void
  2212. dna_list_remove_type(char **list, char *type)
  2213. {
  2214. int i = 0;
  2215. int found_type = 0;
  2216. if (list && type) {
  2217. /* Go through the list until we find the type that
  2218. * we want to remove. We simply free the type we
  2219. * want to remove and shift the remaining array
  2220. * elements down by one index. This will leave us
  2221. * with two NULL elements at the end of the list,
  2222. * but this should not cause any problems. */
  2223. for (i = 0; list[i]; i++) {
  2224. if (found_type) {
  2225. list[i] = list[i + 1];
  2226. } else if (slapi_attr_types_equivalent(type, list[i])) {
  2227. slapi_ch_free_string(&list[i]);
  2228. list[i] = list[i + 1];
  2229. found_type = 1;
  2230. }
  2231. }
  2232. }
  2233. }
  2234. /*
  2235. * dna_is_multitype_range()
  2236. *
  2237. * Returns 1 if the range has multiple types configured.
  2238. * Returns 0 otherwise.
  2239. */
  2240. static int dna_is_multitype_range(struct configEntry *config_entry)
  2241. {
  2242. int ret = 0;
  2243. if (config_entry && config_entry->types && config_entry->types[1]) {
  2244. ret = 1;
  2245. }
  2246. return ret;
  2247. }
  2248. /*
  2249. * dna_create_valcheck_filter()
  2250. *
  2251. * Creates a filter string used to check if a value is free. If
  2252. * filter already holds a valid pointer, it is assumed to have enough
  2253. * space to hold the filter. This allows the same memory to be used
  2254. * over and over when you only want to change the assertion value.
  2255. * If filter contains a NULL pointer, a new string of the appropriate
  2256. * size will be allocated. The caller must free this when finished.
  2257. */
  2258. static void
  2259. dna_create_valcheck_filter(struct configEntry *config_entry, PRUint64 value, char **filter)
  2260. {
  2261. int filterlen = 0;
  2262. int typeslen = 0;
  2263. int i = 0;
  2264. int bytes_out = 0;
  2265. int multitype = 0;
  2266. /* Just return if we didn't get an address for the filter. */
  2267. if (filter == NULL) {
  2268. return;
  2269. }
  2270. /* To determine the filter length, we add together the following:
  2271. *
  2272. * - the string length of the filter in the config
  2273. * - the string length sum of all configured types
  2274. * - 23 bytes for each type (20 for the max string
  2275. * representation of a NSPRIu64, 3 for "(=)"
  2276. * - 3 bytes for the beginning and end of the filter - "(&" and ")"
  2277. * - 3 bytes to OR together multiple types (if present) - "(|" and ")"
  2278. * - the string length of the prefix (if one is configured) for each type
  2279. * - 1 byte for the trailing \0
  2280. *
  2281. * The filter length should be the same every time if the config struct
  2282. * has not been changed. We need to calculate the filter length even
  2283. * if we are reusing memory since we have no other way of knowing the
  2284. * length used when it was originally allocated. We trust the caller
  2285. * to only reuse the pointer with the same config struct.
  2286. */
  2287. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2288. typeslen += strlen(config_entry->types[i]);
  2289. }
  2290. if (i > 1) {
  2291. multitype = 1;
  2292. }
  2293. filterlen = strlen(config_entry->filter) + typeslen +
  2294. (i * 23) + 3 + 1 +
  2295. (config_entry->prefix ? (i * strlen(config_entry->prefix)) : 0) +
  2296. (multitype ? 3 : 0);
  2297. /* Allocate space for the filter if it hasn't been allocated yet. */
  2298. if (*filter == NULL) {
  2299. *filter = slapi_ch_malloc(filterlen);
  2300. }
  2301. /* Write out the beginning of the filter. If multiple types
  2302. * are configured, we need to OR together the search clauses
  2303. * for the types. */
  2304. if (multitype) {
  2305. bytes_out = snprintf(*filter, filterlen, "(&%s(|", config_entry->filter);
  2306. } else {
  2307. bytes_out = snprintf(*filter, filterlen, "(&%s", config_entry->filter);
  2308. }
  2309. /* Loop through the types and append each filter clause. */
  2310. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2311. bytes_out += snprintf(*filter + bytes_out, filterlen - bytes_out,
  2312. "(%s=%s%" PRIu64 ")", config_entry->types[i],
  2313. config_entry->prefix ? config_entry->prefix : "",
  2314. value);
  2315. }
  2316. /* Append the end of the filter. We need an extra paren
  2317. * to close out the OR if we have multiple types. */
  2318. if (multitype) {
  2319. strncat(*filter, "))", filterlen - bytes_out);
  2320. } else {
  2321. strncat(*filter, ")", filterlen - bytes_out);
  2322. }
  2323. }
  2324. /* for mods and adds:
  2325. where dn's are supplied, the closest in scope
  2326. is used as long as the type filter matches
  2327. and the type has not been generated yet.
  2328. */
  2329. static int dna_pre_op(Slapi_PBlock * pb, int modtype)
  2330. {
  2331. char *dn = 0;
  2332. PRCList *list = 0;
  2333. struct configEntry *config_entry = 0;
  2334. struct slapi_entry *e = 0;
  2335. Slapi_Entry *resulting_e = 0;
  2336. char *value = 0;
  2337. char **types_to_generate = NULL;
  2338. char **generated_types = NULL;
  2339. Slapi_Mods *smods = 0;
  2340. Slapi_Mod *smod = 0;
  2341. LDAPMod **mods;
  2342. int free_entry = 0;
  2343. char *errstr = NULL;
  2344. int i = 0;
  2345. int ret = 0;
  2346. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2347. "--> dna_pre_op\n");
  2348. /* Just bail if we aren't ready to service requests yet. */
  2349. if (!g_plugin_started)
  2350. goto bail;
  2351. if (0 == (dn = dna_get_dn(pb)))
  2352. goto bail;
  2353. if (LDAP_CHANGETYPE_ADD == modtype) {
  2354. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  2355. } else {
  2356. /* xxxPAR: Ideally SLAPI_MODIFY_EXISTING_ENTRY should be
  2357. * available but it turns out that is only true if you are
  2358. * a dbm backend pre-op plugin - lucky dbm backend pre-op
  2359. * plugins.
  2360. * I think that is wrong since the entry is useful for filter
  2361. * tests and schema checks and this plugin shouldn't be limited
  2362. * to a single backend type, but I don't want that fight right
  2363. * now so we go get the entry here
  2364. *
  2365. slapi_pblock_get( pb, SLAPI_MODIFY_EXISTING_ENTRY, &e);
  2366. */
  2367. Slapi_DN *tmp_dn = slapi_sdn_new_dn_byref(dn);
  2368. if (tmp_dn) {
  2369. slapi_search_internal_get_entry(tmp_dn, 0, &e, getPluginID());
  2370. slapi_sdn_free(&tmp_dn);
  2371. free_entry = 1;
  2372. }
  2373. /* grab the mods - we'll put them back later with
  2374. * our modifications appended
  2375. */
  2376. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  2377. smods = slapi_mods_new();
  2378. slapi_mods_init_passin(smods, mods);
  2379. /* We need the resulting entry after the mods are applied to
  2380. * see if the entry is within the scope. */
  2381. if (e) {
  2382. resulting_e = slapi_entry_dup(e);
  2383. if (mods && (slapi_entry_apply_mods(resulting_e, mods) != LDAP_SUCCESS)) {
  2384. /* The mods don't apply cleanly, so we just let this op go
  2385. * to let the main server handle it. */
  2386. goto bailmod;
  2387. }
  2388. }
  2389. }
  2390. if (0 == e)
  2391. goto bailmod;
  2392. if (dna_dn_is_config(dn)) {
  2393. /* Validate config changes, but don't apply them.
  2394. * This allows us to reject invalid config changes
  2395. * here at the pre-op stage. Applying the config
  2396. * needs to be done at the post-op stage. */
  2397. Slapi_Entry *test_e = NULL;
  2398. /* For a MOD, we need to check the resulting entry */
  2399. if (LDAP_CHANGETYPE_ADD == modtype) {
  2400. test_e = e;
  2401. } else {
  2402. test_e = resulting_e;
  2403. }
  2404. if (dna_parse_config_entry(test_e, 0) != DNA_SUCCESS) {
  2405. /* Refuse the operation if config parsing failed. */
  2406. ret = LDAP_UNWILLING_TO_PERFORM;
  2407. if (LDAP_CHANGETYPE_ADD == modtype) {
  2408. errstr = slapi_ch_smprintf("Not a valid DNA configuration entry.");
  2409. } else {
  2410. errstr = slapi_ch_smprintf("Changes result in an invalid "
  2411. "DNA configuration.");
  2412. }
  2413. }
  2414. /* We're done, so just bail. */
  2415. goto bailmod;
  2416. }
  2417. dna_read_lock();
  2418. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2419. list = PR_LIST_HEAD(dna_global_config);
  2420. while (list != dna_global_config && LDAP_SUCCESS == ret) {
  2421. config_entry = (struct configEntry *) list;
  2422. /* Did we already service all of these configured types? */
  2423. if (dna_list_contains_types(generated_types, config_entry->types)) {
  2424. goto next;
  2425. }
  2426. /* is the entry in scope? */
  2427. if (config_entry->scope) {
  2428. if (!slapi_dn_issuffix(dn, config_entry->scope))
  2429. goto next;
  2430. }
  2431. /* does the entry match the filter? */
  2432. if (config_entry->slapi_filter) {
  2433. Slapi_Entry *test_e = NULL;
  2434. /* For a MOD operation, we need to check the filter
  2435. * against the resulting entry. */
  2436. if (LDAP_CHANGETYPE_ADD == modtype) {
  2437. test_e = e;
  2438. } else {
  2439. test_e = resulting_e;
  2440. }
  2441. if (LDAP_SUCCESS != slapi_vattr_filter_test(pb,
  2442. test_e,
  2443. config_entry->
  2444. slapi_filter, 0))
  2445. goto next;
  2446. }
  2447. if (LDAP_CHANGETYPE_ADD == modtype) {
  2448. if (config_entry->types && dna_is_multitype_range(config_entry)) {
  2449. /* For a multi-type range, we only generate a value
  2450. * for types where the magic value is set. We do not
  2451. * generate a value for missing types. */
  2452. for (i = 0; config_entry->types && config_entry->types[i]; i++) {
  2453. value = slapi_entry_attr_get_charptr(e, config_entry->types[i]);
  2454. if (value && !slapi_UTF8CASECMP(config_entry->generate, value)) {
  2455. slapi_ch_array_add(&types_to_generate,
  2456. slapi_ch_strdup(config_entry->types[i]));
  2457. }
  2458. slapi_ch_free_string(&value);
  2459. }
  2460. } else {
  2461. /* For a single type range, we generate the value if
  2462. * the magic value is set or if the type is missing. */
  2463. value = slapi_entry_attr_get_charptr(e, config_entry->types[0]);
  2464. if ((value && !slapi_UTF8CASECMP(config_entry->generate, value))
  2465. || 0 == value) {
  2466. slapi_ch_array_add(&types_to_generate,
  2467. slapi_ch_strdup(config_entry->types[0]));
  2468. }
  2469. slapi_ch_free_string(&value);
  2470. }
  2471. } else {
  2472. /* check mods for magic value */
  2473. Slapi_Mod *next_mod = slapi_mod_new();
  2474. smod = slapi_mods_get_first_smod(smods, next_mod);
  2475. while (smod) {
  2476. char *type = (char *)
  2477. slapi_mod_get_type(smod);
  2478. /* See if the type matches any configured type. */
  2479. if (dna_list_contains_type(config_entry->types, type)) {
  2480. /* If all values are being deleted, we need to
  2481. * generate a new value. We don't do this for
  2482. * multi-type ranges since they require the magic
  2483. * value to be specified to trigger generation. */
  2484. if (SLAPI_IS_MOD_DELETE(slapi_mod_get_operation(smod)) &&
  2485. !dna_is_multitype_range(config_entry)) {
  2486. int numvals = slapi_mod_get_num_values(smod);
  2487. if (numvals == 0) {
  2488. slapi_ch_array_add(&types_to_generate,
  2489. slapi_ch_strdup(type));
  2490. } else {
  2491. Slapi_Attr *attr = NULL;
  2492. int e_numvals = 0;
  2493. slapi_entry_attr_find(e, type, &attr);
  2494. if (attr) {
  2495. slapi_attr_get_numvalues(attr, &e_numvals);
  2496. if (numvals >= e_numvals) {
  2497. slapi_ch_array_add(&types_to_generate,
  2498. slapi_ch_strdup(type));
  2499. }
  2500. }
  2501. }
  2502. } else {
  2503. /* This is either adding or replacing a value */
  2504. struct berval *bv = slapi_mod_get_first_value(smod);
  2505. /* If this type is already in the to be generated
  2506. * list, a previous mod in this same modify operation
  2507. * either removed all values or set the magic value.
  2508. * It's possible that this mod is adding a valid value,
  2509. * which means we would not want to generate a new value.
  2510. * It is safe to remove this type from the to be
  2511. * generated list since it will be re-added here if
  2512. * necessary. */
  2513. if (dna_list_contains_type(types_to_generate, type)) {
  2514. dna_list_remove_type(types_to_generate, type);
  2515. }
  2516. /* If we have a value, see if it's the magic value. */
  2517. if (bv) {
  2518. int len = strlen(config_entry->generate);
  2519. if (len == bv->bv_len) {
  2520. if (!slapi_UTF8NCASECMP(bv->bv_val,
  2521. config_entry->generate,
  2522. len)) {
  2523. slapi_ch_array_add(&types_to_generate,
  2524. slapi_ch_strdup(type));
  2525. }
  2526. }
  2527. } else if (!dna_is_multitype_range(config_entry)) {
  2528. /* This is a replace with no new values, so we need
  2529. * to generate a new value if this is not a multi-type
  2530. * range. */
  2531. slapi_ch_array_add(&types_to_generate,
  2532. slapi_ch_strdup(type));
  2533. }
  2534. }
  2535. }
  2536. slapi_mod_done(next_mod);
  2537. smod = slapi_mods_get_next_smod(smods, next_mod);
  2538. }
  2539. slapi_mod_free(&next_mod);
  2540. }
  2541. /* We need to perform one last check for modify operations. If an
  2542. * entry within the scope has not triggered generation yet, we need
  2543. * to see if a value exists for the managed type in the resulting
  2544. * entry. This will catch a modify operation that brings an entry
  2545. * into scope for a managed range, but doesn't supply a value for
  2546. * the managed type. We don't do this for multi-type ranges. */
  2547. if ((LDAP_CHANGETYPE_MODIFY == modtype) && (!types_to_generate ||
  2548. (types_to_generate && !types_to_generate[0])) &&
  2549. !dna_is_multitype_range(config_entry)) {
  2550. Slapi_Attr *attr = NULL;
  2551. if (slapi_entry_attr_find(resulting_e, config_entry->types[0], &attr) != 0) {
  2552. slapi_ch_array_add(&types_to_generate,
  2553. slapi_ch_strdup(config_entry->types[0]));
  2554. }
  2555. }
  2556. if (types_to_generate && types_to_generate[0]) {
  2557. char *new_value;
  2558. int len;
  2559. /* create the value to add */
  2560. ret = dna_get_next_value(config_entry, &value);
  2561. if (DNA_SUCCESS != ret) {
  2562. errstr = slapi_ch_smprintf("Allocation of a new value for range"
  2563. " %s failed! Unable to proceed.",
  2564. config_entry->dn);
  2565. break;
  2566. }
  2567. len = strlen(value) + 1;
  2568. if (config_entry->prefix) {
  2569. len += strlen(config_entry->prefix);
  2570. }
  2571. new_value = slapi_ch_malloc(len);
  2572. if (config_entry->prefix) {
  2573. strcpy(new_value, config_entry->prefix);
  2574. strcat(new_value, value);
  2575. } else
  2576. strcpy(new_value, value);
  2577. /* do the mod */
  2578. if (LDAP_CHANGETYPE_ADD == modtype) {
  2579. /* add - add to entry */
  2580. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2581. slapi_entry_attr_set_charptr(e,
  2582. types_to_generate[i],
  2583. new_value);
  2584. }
  2585. } else {
  2586. /* mod - add to mods */
  2587. for (i = 0; types_to_generate && types_to_generate[i]; i++) {
  2588. slapi_mods_add_string(smods,
  2589. LDAP_MOD_REPLACE,
  2590. types_to_generate[i], new_value);
  2591. }
  2592. }
  2593. /* Make sure we don't generate for this
  2594. * type again by keeping a list of types
  2595. * we have generated for already.
  2596. */
  2597. if (LDAP_SUCCESS == ret) {
  2598. if (generated_types == NULL) {
  2599. /* If we don't have a list of generated types yet,
  2600. * we can just use the types_to_generate list so
  2601. * we don't have to allocate anything. */
  2602. generated_types = types_to_generate;
  2603. types_to_generate = NULL;
  2604. } else {
  2605. /* Just reuse the elements out of types_to_generate for the
  2606. * generated types list to avoid allocating them again. */
  2607. for (i = 0; types_to_generate && types_to_generate[i]; ++i) {
  2608. slapi_ch_array_add(&generated_types, types_to_generate[i]);
  2609. types_to_generate[i] = NULL;
  2610. }
  2611. }
  2612. }
  2613. /* free up */
  2614. slapi_ch_free_string(&value);
  2615. slapi_ch_free_string(&new_value);
  2616. slapi_ch_array_free(types_to_generate);
  2617. }
  2618. next:
  2619. list = PR_NEXT_LINK(list);
  2620. }
  2621. }
  2622. dna_unlock();
  2623. bailmod:
  2624. if (LDAP_CHANGETYPE_MODIFY == modtype) {
  2625. /* Put the updated mods back into place. */
  2626. mods = slapi_mods_get_ldapmods_passout(smods);
  2627. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, mods);
  2628. slapi_mods_free(&smods);
  2629. }
  2630. bail:
  2631. slapi_ch_array_free(generated_types);
  2632. if (free_entry && e)
  2633. slapi_entry_free(e);
  2634. if (resulting_e)
  2635. slapi_entry_free(resulting_e);
  2636. if (ret) {
  2637. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2638. "dna_pre_op: operation failure [%d]\n", ret);
  2639. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  2640. slapi_ch_free((void **)&errstr);
  2641. ret = DNA_FAILURE;
  2642. }
  2643. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2644. "<-- dna_pre_op\n");
  2645. return ret;
  2646. }
  2647. static int dna_add_pre_op(Slapi_PBlock * pb)
  2648. {
  2649. return dna_pre_op(pb, LDAP_CHANGETYPE_ADD);
  2650. }
  2651. static int dna_mod_pre_op(Slapi_PBlock * pb)
  2652. {
  2653. return dna_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  2654. }
  2655. static int dna_config_check_post_op(Slapi_PBlock * pb)
  2656. {
  2657. char *dn;
  2658. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2659. "--> dna_config_check_post_op\n");
  2660. if ((dn = dna_get_dn(pb))) {
  2661. if (dna_dn_is_config(dn))
  2662. dna_load_plugin_config();
  2663. }
  2664. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2665. "<-- dna_config_check_post_op\n");
  2666. return 0;
  2667. }
  2668. /****************************************************
  2669. * Range Extension Extended Operation
  2670. ***************************************************/
  2671. static int dna_extend_exop(Slapi_PBlock *pb)
  2672. {
  2673. int ret = -1;
  2674. struct berval *reqdata = NULL;
  2675. BerElement *tmp_bere = NULL;
  2676. char *shared_dn = NULL;
  2677. char *bind_dn = NULL;
  2678. char *oid = NULL;
  2679. PRUint64 lower = 0;
  2680. PRUint64 upper = 0;
  2681. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2682. "--> dna_extend_exop\n");
  2683. /* Fetch the request OID */
  2684. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_OID, &oid);
  2685. if (!oid) {
  2686. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2687. "dna_extend_exop: Unable to retrieve request OID.\n");
  2688. goto free_and_return;
  2689. }
  2690. /* Make sure the request OID is correct. */
  2691. if (strcmp(oid, DNA_EXTEND_EXOP_REQUEST_OID) != 0) {
  2692. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2693. "dna_extend_exop: Received incorrect request OID.\n");
  2694. goto free_and_return;
  2695. }
  2696. /* Fetch the request data */
  2697. slapi_pblock_get(pb, SLAPI_EXT_OP_REQ_VALUE, &reqdata);
  2698. if (!reqdata) {
  2699. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2700. "dna_extend_exop: No request data received.\n");
  2701. goto free_and_return;
  2702. }
  2703. /* decode the exop */
  2704. if ((tmp_bere = ber_init(reqdata)) == NULL) {
  2705. ret = -1;
  2706. goto free_and_return;
  2707. }
  2708. if (ber_scanf(tmp_bere, "{a}", &shared_dn) == LBER_ERROR) {
  2709. ret = -1;
  2710. goto free_and_return;
  2711. }
  2712. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2713. "dna_extend_exop: received range extension "
  2714. "request for range [%s]\n", shared_dn);
  2715. /* Only allow range requests from the replication bind DN user */
  2716. slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
  2717. if (!dna_is_replica_bind_dn(shared_dn, bind_dn)) {
  2718. ret = LDAP_INSUFFICIENT_ACCESS;
  2719. goto free_and_return;
  2720. }
  2721. /* See if we have the req. range configured.
  2722. * If so, we need to see if we have range to provide. */
  2723. ret = dna_release_range(shared_dn, &lower, &upper);
  2724. if (ret == LDAP_SUCCESS) {
  2725. /* We have range to give away, so construct
  2726. * and send the response. */
  2727. BerElement *respber = NULL;
  2728. struct berval *respdata = NULL;
  2729. struct berval range_low = {0, NULL};
  2730. struct berval range_high = {0, NULL};
  2731. char lowstr[16];
  2732. char highstr[16];
  2733. /* Create the exop response */
  2734. PR_snprintf(lowstr, sizeof(lowstr), "%" NSPRIu64, lower);
  2735. PR_snprintf(highstr, sizeof(highstr), "%" NSPRIu64, upper);
  2736. range_low.bv_val = lowstr;
  2737. range_low.bv_len = strlen(range_low.bv_val);
  2738. range_high.bv_val = highstr;
  2739. range_high.bv_len = strlen(range_high.bv_val);
  2740. if ((respber = ber_alloc()) == NULL) {
  2741. ret = LDAP_NO_MEMORY;
  2742. goto free_and_return;
  2743. }
  2744. if (LBER_ERROR == (ber_printf(respber, "{oo}",
  2745. range_low.bv_val, range_low.bv_len,
  2746. range_high.bv_val, range_high.bv_len))) {
  2747. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2748. "dna_extend_exop: Unable to encode exop response.\n");
  2749. ber_free(respber, 1);
  2750. ret = LDAP_ENCODING_ERROR;
  2751. goto free_and_return;
  2752. }
  2753. ber_flatten(respber, &respdata);
  2754. ber_free(respber, 1);
  2755. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_OID, DNA_EXTEND_EXOP_RESPONSE_OID);
  2756. slapi_pblock_set(pb, SLAPI_EXT_OP_RET_VALUE, respdata);
  2757. /* send the response ourselves */
  2758. slapi_send_ldap_result( pb, ret, NULL, NULL, 0, NULL );
  2759. ret = SLAPI_PLUGIN_EXTENDED_SENT_RESULT;
  2760. ber_bvfree(respdata);
  2761. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2762. "dna_extend_exop: Released range %" NSPRIu64 "-%" NSPRIu64 ".\n",
  2763. lower, upper);
  2764. }
  2765. free_and_return:
  2766. slapi_ch_free_string(&shared_dn);
  2767. slapi_ch_free_string(&bind_dn);
  2768. if (NULL != tmp_bere) {
  2769. ber_free(tmp_bere, 1);
  2770. tmp_bere = NULL;
  2771. }
  2772. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2773. "<-- dna_extend_exop\n");
  2774. return ret;
  2775. }
  2776. /*
  2777. * dna_release_range()
  2778. *
  2779. * Checks if we have any values that we can release
  2780. * for the range specified by range_dn.
  2781. */
  2782. static int
  2783. dna_release_range(char *range_dn, PRUint64 *lower, PRUint64 *upper)
  2784. {
  2785. int ret = 0;
  2786. int match = 0;
  2787. PRCList *list = NULL;
  2788. Slapi_DN *cfg_base_sdn = NULL;
  2789. Slapi_DN *range_sdn = NULL;
  2790. struct configEntry *config_entry = NULL;
  2791. int set_extend_flag = 0;
  2792. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2793. "--> dna_release_range\n");
  2794. if (range_dn) {
  2795. range_sdn = slapi_sdn_new_dn_byref(range_dn);
  2796. dna_read_lock();
  2797. /* Go through the config entries to see if we
  2798. * have a shared range configured that matches
  2799. * the range from the exop request. */
  2800. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2801. list = PR_LIST_HEAD(dna_global_config);
  2802. while ((list != dna_global_config) && match != 1) {
  2803. config_entry = (struct configEntry *)list;
  2804. cfg_base_sdn = slapi_sdn_new_dn_byref(config_entry->shared_cfg_base);
  2805. if (slapi_sdn_compare(cfg_base_sdn, range_sdn) == 0) {
  2806. /* We found a match. Set match flag to
  2807. * break out of the loop. */
  2808. match = 1;
  2809. } else {
  2810. config_entry = NULL;
  2811. list = PR_NEXT_LINK(list);
  2812. }
  2813. slapi_sdn_free(&cfg_base_sdn);
  2814. }
  2815. }
  2816. /* config_entry will point to our match if we found one */
  2817. if (config_entry) {
  2818. int release = 0;
  2819. Slapi_PBlock *pb = NULL;
  2820. LDAPMod mod_replace;
  2821. LDAPMod *mods[2];
  2822. char *replace_val[2];
  2823. /* 16 for max 64-bit unsigned plus the trailing '\0' */
  2824. char max_value[17];
  2825. /* Need to bail if we're performing a range request
  2826. * for this range. This is to prevent the case where two
  2827. * servers are asking each other for more range for the
  2828. * same managed range. This would result in a network
  2829. * deadlock until the idletimeout kills one of the
  2830. * connections. */
  2831. slapi_lock_mutex(config_entry->extend_lock);
  2832. if (config_entry->extend_in_progress) {
  2833. /* We're already processing a range extention, so bail. */
  2834. slapi_log_error(SLAPI_LOG_PLUGIN, DNA_PLUGIN_SUBSYSTEM,
  2835. "dna_release_range: Already processing a "
  2836. "range extension request. Skipping request.\n");
  2837. slapi_unlock_mutex(config_entry->extend_lock);
  2838. ret = LDAP_UNWILLING_TO_PERFORM;
  2839. goto bail;
  2840. } else {
  2841. /* Set a flag indicating that we're attempting to extend this range */
  2842. config_entry->extend_in_progress = 1;
  2843. set_extend_flag = 1;
  2844. slapi_unlock_mutex(config_entry->extend_lock);
  2845. }
  2846. /* Obtain the lock for this range */
  2847. slapi_lock_mutex(config_entry->lock);
  2848. /* Refuse if we're at or below our threshold */
  2849. if (config_entry->remaining <= config_entry->threshold) {
  2850. ret = LDAP_UNWILLING_TO_PERFORM;
  2851. goto bail;
  2852. }
  2853. /* If we have a next range, we need to give up values from
  2854. * it instead of from the active range */
  2855. if (config_entry->next_range_lower != 0) {
  2856. /* Release up to half of our values from the next range. */
  2857. release = (((config_entry->next_range_upper - config_entry->next_range_lower + 1) /
  2858. 2) / config_entry->threshold) * config_entry->threshold;
  2859. if (release == 0) {
  2860. ret = LDAP_UNWILLING_TO_PERFORM;
  2861. goto bail;
  2862. }
  2863. *upper = config_entry->next_range_upper;
  2864. *lower = *upper - release + 1;
  2865. /* Try to set the new next range in the config */
  2866. ret = dna_update_next_range(config_entry, config_entry->next_range_lower,
  2867. *lower - 1);
  2868. } else {
  2869. /* We release up to half of our remaining values,
  2870. * but we'll only release a range that is a multiple
  2871. * of our threshold. */
  2872. release = ((config_entry->remaining / 2) /
  2873. config_entry->threshold) * config_entry->threshold;
  2874. if (release == 0) {
  2875. ret = LDAP_UNWILLING_TO_PERFORM;
  2876. goto bail;
  2877. }
  2878. /* We give away values from the upper end of our range. */
  2879. *upper = config_entry->maxval;
  2880. *lower = *upper - release + 1;
  2881. /* try to set the new maxval in the config entry */
  2882. PR_snprintf(max_value, sizeof(max_value),"%" NSPRIu64, (*lower - 1));
  2883. /* set up our replace modify operation */
  2884. replace_val[0] = max_value;
  2885. replace_val[1] = 0;
  2886. mod_replace.mod_op = LDAP_MOD_REPLACE;
  2887. mod_replace.mod_type = DNA_MAXVAL;
  2888. mod_replace.mod_values = replace_val;
  2889. mods[0] = &mod_replace;
  2890. mods[1] = 0;
  2891. pb = slapi_pblock_new();
  2892. if (NULL == pb) {
  2893. ret = LDAP_OPERATIONS_ERROR;
  2894. goto bail;
  2895. }
  2896. slapi_modify_internal_set_pb(pb, config_entry->dn,
  2897. mods, 0, 0, getPluginID(), 0);
  2898. slapi_modify_internal_pb(pb);
  2899. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  2900. slapi_pblock_destroy(pb);
  2901. pb = NULL;
  2902. if (ret == LDAP_SUCCESS) {
  2903. /* Adjust maxval in our cached config and shared config */
  2904. config_entry->maxval = *lower - 1;
  2905. dna_notice_allocation(config_entry, config_entry->nextval, 0, 0);
  2906. }
  2907. }
  2908. if (ret != LDAP_SUCCESS) {
  2909. /* Updating the config failed, so reset. We don't
  2910. * want to give the caller any range */
  2911. *lower = 0;
  2912. *upper = 0;
  2913. slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
  2914. "dna_release_range: Error updating "
  2915. "configuration entry [err=%d]\n", ret);
  2916. }
  2917. }
  2918. bail:
  2919. if (set_extend_flag) {
  2920. slapi_lock_mutex(config_entry->extend_lock);
  2921. config_entry->extend_in_progress = 0;
  2922. slapi_unlock_mutex(config_entry->extend_lock);
  2923. }
  2924. if (config_entry) {
  2925. slapi_unlock_mutex(config_entry->lock);
  2926. }
  2927. slapi_sdn_free(&range_sdn);
  2928. dna_unlock();
  2929. }
  2930. slapi_log_error(SLAPI_LOG_TRACE, DNA_PLUGIN_SUBSYSTEM,
  2931. "<-- dna_release_range\n");
  2932. return ret;
  2933. }
  2934. /****************************************************
  2935. End of
  2936. Functions that actually do things other
  2937. than config and startup
  2938. ****************************************************/
  2939. /**
  2940. * debug functions to print config
  2941. */
  2942. void dna_dump_config()
  2943. {
  2944. PRCList *list;
  2945. dna_read_lock();
  2946. if (!PR_CLIST_IS_EMPTY(dna_global_config)) {
  2947. list = PR_LIST_HEAD(dna_global_config);
  2948. while (list != dna_global_config) {
  2949. dna_dump_config_entry((struct configEntry *) list);
  2950. list = PR_NEXT_LINK(list);
  2951. }
  2952. }
  2953. dna_unlock();
  2954. }
  2955. void dna_dump_config_entry(struct configEntry * entry)
  2956. {
  2957. int i = 0;
  2958. for (i = 0; entry->types && entry->types[i]; i++) {
  2959. printf("<---- type -----------> %s\n", entry->types[i]);
  2960. }
  2961. printf("<---- filter ---------> %s\n", entry->filter);
  2962. printf("<---- prefix ---------> %s\n", entry->prefix);
  2963. printf("<---- scope ----------> %s\n", entry->scope);
  2964. printf("<---- next value -----> %" PRIu64 "\n", entry->nextval);
  2965. printf("<---- max value ------> %" PRIu64 "\n", entry->maxval);
  2966. printf("<---- interval -------> %" PRIu64 "\n", entry->interval);
  2967. printf("<---- generate flag --> %s\n", entry->generate);
  2968. printf("<---- shared cfg base > %s\n", entry->shared_cfg_base);
  2969. printf("<---- shared cfg DN --> %s\n", entry->shared_cfg_dn);
  2970. printf("<---- threshold ------> %" PRIu64 "", entry->threshold);
  2971. }