dna.c 106 KB

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