dna.c 103 KB

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