dna.c 102 KB

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