dna.c 136 KB

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