dna.c 118 KB

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