mep.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  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) 2010 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * Managed Entries Plug-in
  42. */
  43. #include "mep.h"
  44. #include "slapi-private.h"
  45. /*
  46. * Plug-in globals
  47. */
  48. static PRCList *g_mep_config = NULL;
  49. static Slapi_RWLock *g_mep_config_lock;
  50. static void *_PluginID = NULL;
  51. static Slapi_DN *_PluginDN = NULL;
  52. static Slapi_DN *_ConfigAreaDN = NULL;
  53. static Slapi_PluginDesc pdesc = { MEP_FEATURE_DESC,
  54. VENDOR,
  55. DS_PACKAGE_VERSION,
  56. MEP_PLUGIN_DESC };
  57. /*
  58. * Plug-in management functions
  59. */
  60. int mep_init(Slapi_PBlock * pb);
  61. static int mep_start(Slapi_PBlock * pb);
  62. static int mep_close(Slapi_PBlock * pb);
  63. static int mep_postop_init(Slapi_PBlock * pb);
  64. static int mep_internal_postop_init(Slapi_PBlock *pb);
  65. /*
  66. * Operation callbacks (where the real work is done)
  67. */
  68. static int mep_mod_post_op(Slapi_PBlock *pb);
  69. static int mep_add_post_op(Slapi_PBlock *pb);
  70. static int mep_del_post_op(Slapi_PBlock *pb);
  71. static int mep_modrdn_post_op(Slapi_PBlock *pb);
  72. static int mep_pre_op(Slapi_PBlock *pb, int modop);
  73. static int mep_mod_pre_op(Slapi_PBlock *pb);
  74. static int mep_add_pre_op(Slapi_PBlock *pb);
  75. static int mep_del_pre_op(Slapi_PBlock *pb);
  76. static int mep_modrdn_pre_op(Slapi_PBlock *pb);
  77. /*
  78. * Config cache management functions
  79. */
  80. static int mep_load_config();
  81. static void mep_delete_config();
  82. static int mep_parse_config_entry(Slapi_Entry * e, int apply);
  83. static void mep_free_config_entry(struct configEntry ** entry);
  84. /*
  85. * helpers
  86. */
  87. static Slapi_DN *mep_get_sdn(Slapi_PBlock * pb);
  88. static Slapi_DN *mep_get_config_area();
  89. static void mep_set_config_area(Slapi_DN *sdn);
  90. static int mep_dn_is_config(Slapi_DN *sdn);
  91. static int mep_dn_is_template(Slapi_DN *dn);
  92. static void mep_find_config(Slapi_Entry *e, struct configEntry **config);
  93. static void mep_find_config_by_template_dn(Slapi_DN *template_dn,
  94. struct configEntry **config);
  95. static int mep_oktodo(Slapi_PBlock *pb);
  96. static int mep_isrepl(Slapi_PBlock *pb);
  97. static Slapi_Entry *mep_create_managed_entry(struct configEntry *config,
  98. Slapi_Entry *origin);
  99. static void mep_add_managed_entry(struct configEntry *config,
  100. Slapi_Entry *origin);
  101. static void mep_rename_managed_entry(Slapi_Entry *origin,
  102. Slapi_DN *new_dn, Slapi_DN *old_dn);
  103. static Slapi_Mods *mep_get_mapped_mods(struct configEntry *config,
  104. Slapi_Entry *origin, char **mapped_dn);
  105. static int mep_parse_mapped_attr(char *mapping, Slapi_Entry *origin,
  106. char **type, char **value);
  107. static int mep_is_managed_entry(Slapi_Entry *e);
  108. static int mep_is_mapped_attr(Slapi_Entry *template, char *type);
  109. static int mep_has_tombstone_value(Slapi_Entry * e);
  110. static int mep_parse_mapped_origin_attr(char *mapping, char **origin_type);
  111. static int mep_is_mapped_origin_attr(char **vals, char *type);
  112. static char** mep_extract_origin_attrs(Slapi_Entry *entry);
  113. /*
  114. * Config cache locking functions
  115. */
  116. void
  117. mep_config_read_lock()
  118. {
  119. slapi_rwlock_rdlock(g_mep_config_lock);
  120. }
  121. void
  122. mep_config_write_lock()
  123. {
  124. slapi_rwlock_wrlock(g_mep_config_lock);
  125. }
  126. void
  127. mep_config_unlock()
  128. {
  129. slapi_rwlock_unlock(g_mep_config_lock);
  130. }
  131. /*
  132. * Plugin identity functions
  133. */
  134. void
  135. mep_set_plugin_id(void *pluginID)
  136. {
  137. _PluginID = pluginID;
  138. }
  139. void *
  140. mep_get_plugin_id()
  141. {
  142. return _PluginID;
  143. }
  144. void
  145. mep_set_plugin_sdn(Slapi_DN *pluginDN)
  146. {
  147. _PluginDN = pluginDN;
  148. }
  149. Slapi_DN *
  150. mep_get_plugin_sdn()
  151. {
  152. return _PluginDN;
  153. }
  154. static int plugin_is_betxn = 0;
  155. /*
  156. * Plug-in initialization functions
  157. */
  158. int
  159. mep_init(Slapi_PBlock *pb)
  160. {
  161. int status = 0;
  162. char *plugin_identity = NULL;
  163. Slapi_Entry *plugin_entry = NULL;
  164. char *plugin_type = NULL;
  165. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  166. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  167. int predel = SLAPI_PLUGIN_PRE_DELETE_FN;
  168. int premdn = SLAPI_PLUGIN_PRE_MODRDN_FN;
  169. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  170. "--> mep_init\n");
  171. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  172. plugin_entry &&
  173. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  174. plugin_type && strstr(plugin_type, "betxn")) {
  175. plugin_is_betxn = 1;
  176. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  177. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  178. predel = SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN;
  179. premdn = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
  180. }
  181. slapi_ch_free_string(&plugin_type);
  182. /* Store the plugin identity for later use.
  183. * Used for internal operations. */
  184. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  185. PR_ASSERT(plugin_identity);
  186. mep_set_plugin_id(plugin_identity);
  187. /* Register callbacks */
  188. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  189. SLAPI_PLUGIN_VERSION_01) != 0 ||
  190. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  191. (void *) mep_start) != 0 ||
  192. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  193. (void *) mep_close) != 0 ||
  194. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  195. (void *) &pdesc) != 0 ||
  196. slapi_pblock_set(pb, premod, (void *) mep_mod_pre_op) != 0 ||
  197. slapi_pblock_set(pb, preadd, (void *) mep_add_pre_op) != 0 ||
  198. slapi_pblock_set(pb, predel, (void *) mep_del_pre_op) != 0 ||
  199. slapi_pblock_set(pb, premdn, (void *) mep_modrdn_pre_op) != 0) {
  200. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  201. "mep_init: failed to register plugin\n");
  202. status = -1;
  203. }
  204. if (!status && !plugin_is_betxn &&
  205. slapi_register_plugin("internalpostoperation", /* op type */
  206. 1, /* Enabled */
  207. "mep_init", /* this function desc */
  208. mep_internal_postop_init, /* init func */
  209. MEP_INT_POSTOP_DESC, /* plugin desc */
  210. NULL, /* ? */
  211. plugin_identity /* access control */
  212. )) {
  213. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  214. "mep_init: failed to register internalpostoperation plugin\n");
  215. status = -1;
  216. }
  217. if (!status) {
  218. plugin_type = plugin_is_betxn ? "betxnpostoperation" : "postoperation";
  219. if (slapi_register_plugin(plugin_type, /* op type */
  220. 1, /* Enabled */
  221. "mep_init", /* this function desc */
  222. mep_postop_init, /* init func for post op */
  223. MEP_POSTOP_DESC, /* plugin desc */
  224. NULL, /* ? */
  225. plugin_identity /* access control */
  226. )) {
  227. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  228. "mep_init: failed to register plugin\n");
  229. status = -1;
  230. }
  231. }
  232. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  233. "<-- mep_init\n");
  234. return status;
  235. }
  236. /* not used when using plugin as a betxn plugin - betxn plugins are called for both internal and external ops */
  237. static int
  238. mep_internal_postop_init(Slapi_PBlock *pb)
  239. {
  240. int status = 0;
  241. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  242. SLAPI_PLUGIN_VERSION_01) != 0 ||
  243. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  244. (void *) &pdesc) != 0 ||
  245. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  246. (void *) mep_add_post_op) != 0 ||
  247. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  248. (void *) mep_del_post_op) != 0 ||
  249. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  250. (void *) mep_mod_post_op) != 0 ||
  251. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  252. (void *) mep_modrdn_post_op) != 0) {
  253. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  254. "mep_internal_postop_init: failed to register plugin\n");
  255. status = -1;
  256. }
  257. return status;
  258. }
  259. static int
  260. mep_postop_init(Slapi_PBlock *pb)
  261. {
  262. int status = 0;
  263. int addfn = SLAPI_PLUGIN_POST_ADD_FN;
  264. int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
  265. int modfn = SLAPI_PLUGIN_POST_MODIFY_FN;
  266. int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
  267. if (plugin_is_betxn) {
  268. addfn = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  269. delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  270. modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  271. mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  272. }
  273. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  274. SLAPI_PLUGIN_VERSION_01) != 0 ||
  275. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  276. (void *) &pdesc) != 0 ||
  277. slapi_pblock_set(pb, addfn, (void *) mep_add_post_op) != 0 ||
  278. slapi_pblock_set(pb, delfn, (void *) mep_del_post_op) != 0 ||
  279. slapi_pblock_set(pb, modfn, (void *) mep_mod_post_op) != 0 ||
  280. slapi_pblock_set(pb, mdnfn, (void *) mep_modrdn_post_op) != 0) {
  281. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  282. "mep_postop_init: failed to register plugin\n");
  283. status = -1;
  284. }
  285. return status;
  286. }
  287. /*
  288. * mep_start()
  289. *
  290. * Creates config lock and loads config cache.
  291. */
  292. static int
  293. mep_start(Slapi_PBlock * pb)
  294. {
  295. Slapi_DN *plugindn = NULL;
  296. char *config_area = NULL;
  297. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  298. "--> mep_start\n");
  299. g_mep_config_lock = slapi_new_rwlock();
  300. if (!g_mep_config_lock) {
  301. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  302. "mep_start: lock creation failed\n");
  303. return -1;
  304. }
  305. /*
  306. * Get the plug-in target dn from the system
  307. * and store it for future use. */
  308. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &plugindn);
  309. if (NULL == plugindn || 0 == slapi_sdn_get_ndn_len(plugindn)) {
  310. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  311. "mep_start: unable to retrieve plugin dn\n");
  312. return -1;
  313. }
  314. mep_set_plugin_sdn(slapi_sdn_dup(plugindn));
  315. /* Set the alternate config area if one is defined. */
  316. slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
  317. if (config_area) {
  318. mep_set_config_area(slapi_sdn_new_normdn_byval(config_area));
  319. }
  320. /*
  321. * Load the config cache
  322. */
  323. g_mep_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  324. PR_INIT_CLIST(g_mep_config);
  325. if (mep_load_config() != 0) {
  326. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  327. "mep_start: unable to load plug-in configuration\n");
  328. return -1;
  329. }
  330. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  331. "managed entries plug-in: ready for service\n");
  332. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  333. "<-- mep_start\n");
  334. return 0;
  335. }
  336. /*
  337. * mep_close()
  338. *
  339. * Cleans up the config cache.
  340. */
  341. static int
  342. mep_close(Slapi_PBlock * pb)
  343. {
  344. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  345. "--> mep_close\n");
  346. mep_delete_config();
  347. slapi_destroy_rwlock(g_mep_config_lock);
  348. g_mep_config_lock = NULL;
  349. slapi_ch_free((void **)&g_mep_config);
  350. slapi_sdn_free(&_PluginDN);
  351. slapi_sdn_free(&_ConfigAreaDN);
  352. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  353. "<-- mep_close\n");
  354. return 0;
  355. }
  356. PRCList *
  357. mep_get_config()
  358. {
  359. return g_mep_config;
  360. }
  361. /*
  362. * config looks like this
  363. * - cn=Managed Entries,cn=plugins,cn=config
  364. * --- cn=user private groups,...
  365. * --- cn=etc,...
  366. */
  367. static int
  368. mep_load_config()
  369. {
  370. int status = 0;
  371. int result;
  372. int i;
  373. Slapi_PBlock *search_pb;
  374. Slapi_Entry **entries = NULL;
  375. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  376. "--> mep_load_config\n");
  377. /* Clear out any old config. */
  378. mep_config_write_lock();
  379. mep_delete_config();
  380. search_pb = slapi_pblock_new();
  381. /* If an alternate config area is configured, find
  382. * the config entries that are beneath it, otherwise
  383. * we load the entries beneath our top-level plug-in
  384. * config entry. */
  385. if (mep_get_config_area()) {
  386. /* Find the config entries beneath the alternate config area. */
  387. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  388. "mep_load_config: Looking for config entries "
  389. "beneath \"%s\".\n", slapi_sdn_get_ndn(mep_get_config_area()));
  390. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(mep_get_config_area()),
  391. LDAP_SCOPE_SUBTREE, "objectclass=*",
  392. NULL, 0, NULL, NULL, mep_get_plugin_id(), 0);
  393. } else {
  394. /* Find the config entries beneath our plugin entry. */
  395. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  396. "mep_load_config: Looking for config entries "
  397. "beneath \"%s\".\n",
  398. slapi_sdn_get_ndn(mep_get_plugin_sdn()));
  399. slapi_search_internal_set_pb(search_pb,
  400. slapi_sdn_get_ndn(mep_get_plugin_sdn()),
  401. LDAP_SCOPE_SUBTREE, "objectclass=*",
  402. NULL, 0, NULL, NULL, mep_get_plugin_id(), 0);
  403. }
  404. slapi_search_internal_pb(search_pb);
  405. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  406. if (LDAP_SUCCESS != result) {
  407. if (mep_get_config_area() && (result == LDAP_NO_SUCH_OBJECT)) {
  408. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  409. "mep_load_config: Config container \"%s\" does "
  410. "not exist.\n", slapi_sdn_get_ndn(mep_get_config_area()));
  411. goto cleanup;
  412. } else {
  413. status = -1;
  414. goto cleanup;
  415. }
  416. }
  417. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  418. &entries);
  419. /* Loop through all of the entries we found and parse them. */
  420. for (i = 0; entries && (entries[i] != NULL); i++) {
  421. /* We don't care about the status here because we may have
  422. * some invalid config entries, but we just want to continue
  423. * looking for valid ones. */
  424. mep_parse_config_entry(entries[i], 1);
  425. }
  426. cleanup:
  427. slapi_free_search_results_internal(search_pb);
  428. slapi_pblock_destroy(search_pb);
  429. mep_config_unlock();
  430. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  431. "<-- mep_load_config\n");
  432. return status;
  433. }
  434. /*
  435. * mep_parse_config_entry()
  436. *
  437. * Parses a single config entry. If apply is non-zero, then
  438. * we will load and start using the new config. You can simply
  439. * validate config without making any changes by setting apply
  440. * to 0.
  441. *
  442. * Returns 0 if the entry is valid and -1 if it is invalid.
  443. */
  444. static int
  445. mep_parse_config_entry(Slapi_Entry * e, int apply)
  446. {
  447. char *value;
  448. struct configEntry *entry = NULL;
  449. struct configEntry *config_entry;
  450. PRCList *list;
  451. int entry_added = 0;
  452. int ret = 0;
  453. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  454. "--> mep_parse_config_entry\n");
  455. /* If this is the main plug-in
  456. * config entry, just bail. */
  457. if ((slapi_sdn_compare(mep_get_plugin_sdn(), slapi_entry_get_sdn(e)) == 0) ||
  458. (mep_get_config_area() && (slapi_sdn_compare(mep_get_config_area(),
  459. slapi_entry_get_sdn(e)) == 0))) {
  460. ret = -1;
  461. goto bail;
  462. }
  463. entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  464. if (NULL == entry) {
  465. ret = -1;
  466. goto bail;
  467. }
  468. entry->sdn = slapi_sdn_dup(slapi_entry_get_sdn(e));
  469. if(entry->sdn == NULL){
  470. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  471. "mep_parse_config_entry: Error "
  472. "reading dn from config entry\n");
  473. ret = -1;
  474. goto bail;
  475. }
  476. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  477. "----------> dn [%s]\n", slapi_sdn_get_dn(entry->sdn));
  478. /* Load the origin scope */
  479. value = slapi_entry_attr_get_charptr(e, MEP_SCOPE_TYPE);
  480. if (value) {
  481. entry->origin_scope = value;
  482. } else {
  483. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  484. "mep_parse_config_entry: The %s config "
  485. "setting is required for config entry \"%s\".\n",
  486. MEP_SCOPE_TYPE, slapi_sdn_get_dn(entry->sdn));
  487. ret = -1;
  488. goto bail;
  489. }
  490. /* Load the origin filter */
  491. value = slapi_entry_attr_get_charptr(e, MEP_FILTER_TYPE);
  492. if (value) {
  493. /* Convert to a Slapi_Filter to improve performance. */
  494. if (NULL == (entry->origin_filter = slapi_str2filter(value))) {
  495. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM ,
  496. "mep_parse_config_entry: Invalid search filter in "
  497. "%s config setting for config entry \"%s\" "
  498. "(filter = \"%s\").\n", MEP_FILTER_TYPE, slapi_sdn_get_dn(entry->sdn), value);
  499. ret = -1;
  500. }
  501. slapi_ch_free_string(&value);
  502. if (ret != 0) {
  503. goto bail;
  504. }
  505. } else {
  506. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  507. "mep_parse_config_entry: The %s config "
  508. "setting is required for config entry \"%s\".\n",
  509. MEP_FILTER_TYPE, slapi_sdn_get_dn(entry->sdn));
  510. ret = -1;
  511. goto bail;
  512. }
  513. /* Load the managed base */
  514. value = slapi_entry_attr_get_charptr(e, MEP_MANAGED_BASE_TYPE);
  515. if (value) {
  516. entry->managed_base = value;
  517. } else {
  518. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  519. "mep_parse_config_entry: The %s config "
  520. "setting is required for config entry \"%s\".\n",
  521. MEP_MANAGED_BASE_TYPE, slapi_sdn_get_dn(entry->sdn));
  522. ret = -1;
  523. goto bail;
  524. }
  525. /* Load the managed template */
  526. value = slapi_entry_attr_get_charptr(e, MEP_MANAGED_TEMPLATE_TYPE);
  527. if (value) {
  528. Slapi_Entry *test_entry = NULL;
  529. entry->template_sdn = slapi_sdn_new_dn_passin(value);
  530. value = NULL; /* entry->template_sdn owns value now */
  531. /* Fetch the managed entry template */
  532. slapi_search_internal_get_entry(entry->template_sdn, 0,
  533. &entry->template_entry, mep_get_plugin_id());
  534. if (entry->template_entry == NULL) {
  535. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  536. "mep_parse_config_entry: The managed entry "
  537. "template \"%s\" does not exist. Please "
  538. "add it or correct the %s config setting for "
  539. "config entry \"%s\"\n", slapi_sdn_get_dn(entry->template_sdn),
  540. MEP_MANAGED_TEMPLATE_TYPE, slapi_sdn_get_dn(entry->sdn));
  541. ret = -1;
  542. goto bail;
  543. }
  544. /* Validate the template entry by creating a test managed
  545. * entry and running a schema check on it */
  546. test_entry = mep_create_managed_entry(entry, NULL);
  547. if (test_entry == NULL) {
  548. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  549. "mep_parse_config_entry: Unable to create "
  550. "a test managed entry from managed entry "
  551. "template \"%s\". Please check the template "
  552. "entry for errors.\n", slapi_sdn_get_dn(entry->template_sdn));
  553. ret = -1;
  554. goto bail;
  555. }
  556. /* Check the schema */
  557. if (slapi_entry_schema_check(NULL, test_entry) != 0) {
  558. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  559. "mep_parse_config_entry: Test managed "
  560. "entry created from managed entry template "
  561. "\"%s\" violates the schema. Please check "
  562. "the template entry for schema errors.\n",
  563. slapi_sdn_get_dn(entry->template_sdn));
  564. slapi_entry_free(test_entry);
  565. ret = -1;
  566. goto bail;
  567. }
  568. /*
  569. * Extract the origin attrs from the template entry
  570. */
  571. entry->origin_attrs = mep_extract_origin_attrs(entry->template_entry);
  572. /* Dispose of the test entry */
  573. slapi_entry_free(test_entry);
  574. } else {
  575. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  576. "mep_parse_config_entry: The %s config "
  577. "setting is required for config entry \"%s\".\n",
  578. MEP_MANAGED_TEMPLATE_TYPE, slapi_sdn_get_dn(entry->sdn));
  579. ret = -1;
  580. goto bail;
  581. }
  582. /* If we were only called to validate config, we can
  583. * just bail out before applying the config changes */
  584. if (apply == 0) {
  585. goto bail;
  586. }
  587. /* Add the config object to the list. We order by scope. */
  588. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  589. list = PR_LIST_HEAD(g_mep_config);
  590. while (list != g_mep_config) {
  591. config_entry = (struct configEntry *) list;
  592. /* If the config entry we are adding has a scope that is
  593. * a child of the scope of the current list item, we insert
  594. * the entry before that list item. */
  595. if (slapi_dn_issuffix(entry->origin_scope, config_entry->origin_scope)) {
  596. PR_INSERT_BEFORE(&(entry->list), list);
  597. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  598. "store [%s] before [%s] \n", slapi_sdn_get_dn(entry->sdn),
  599. slapi_sdn_get_dn(config_entry->sdn));
  600. entry_added = 1;
  601. break;
  602. }
  603. list = PR_NEXT_LINK(list);
  604. /* If we hit the end of the list, add to the tail. */
  605. if (g_mep_config == list) {
  606. PR_INSERT_BEFORE(&(entry->list), list);
  607. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  608. "store [%s] at tail\n", slapi_sdn_get_dn(entry->sdn));
  609. entry_added = 1;
  610. break;
  611. }
  612. }
  613. } else {
  614. /* first entry */
  615. PR_INSERT_LINK(&(entry->list), g_mep_config);
  616. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  617. "store [%s] at head \n", slapi_sdn_get_dn(entry->sdn));
  618. entry_added = 1;
  619. }
  620. bail:
  621. if (0 == entry_added) {
  622. /* Don't log error if we weren't asked to apply config */
  623. if ((apply != 0) && (entry != NULL)) {
  624. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  625. "mep_parse_config_entry: Invalid config entry "
  626. "[%s] skipped\n", slapi_sdn_get_dn(entry->sdn));
  627. }
  628. mep_free_config_entry(&entry);
  629. } else {
  630. ret = 0;
  631. }
  632. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  633. "<-- mep_parse_config_entry\n");
  634. return ret;
  635. }
  636. static void
  637. mep_free_config_entry(struct configEntry ** entry)
  638. {
  639. struct configEntry *e = *entry;
  640. if (e == NULL)
  641. return;
  642. if (e->sdn) {
  643. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  644. "freeing config entry [%s]\n", slapi_sdn_get_dn(e->sdn));
  645. slapi_sdn_free(&e->sdn);
  646. }
  647. if (e->origin_scope) {
  648. slapi_ch_free_string(&e->origin_scope);
  649. }
  650. if (e->origin_filter) {
  651. slapi_filter_free(e->origin_filter, 1);
  652. }
  653. if (e->managed_base) {
  654. slapi_ch_free_string(&e->managed_base);
  655. }
  656. if (e->template_sdn) {
  657. slapi_sdn_free(&e->template_sdn);
  658. }
  659. if (e->template_entry) {
  660. slapi_entry_free(e->template_entry);
  661. }
  662. if(e->origin_attrs){
  663. slapi_ch_array_free(e->origin_attrs);
  664. }
  665. slapi_ch_free((void **) entry);
  666. }
  667. static void
  668. mep_delete_configEntry(PRCList *entry)
  669. {
  670. PR_REMOVE_LINK(entry);
  671. mep_free_config_entry((struct configEntry **) &entry);
  672. }
  673. static void
  674. mep_delete_config()
  675. {
  676. PRCList *list;
  677. /* Delete the config cache. */
  678. while (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  679. list = PR_LIST_HEAD(g_mep_config);
  680. mep_delete_configEntry(list);
  681. }
  682. return;
  683. }
  684. /*
  685. * Helper functions
  686. */
  687. /*
  688. * mep_parse_mapped_origin_attr()
  689. *
  690. * Parses a mapped attribute setting from a template and
  691. * grabs the attribute name and places it in origin_type.
  692. *
  693. * This is used to determine if a modify operation needs
  694. * to update the managed entry.
  695. */
  696. static int
  697. mep_parse_mapped_origin_attr(char *mapping, char **origin_type)
  698. {
  699. int ret = 0;
  700. char *p = NULL;
  701. char *end = NULL;
  702. char *var_start = NULL;
  703. /* reset the pointer for origin_type as this func is usually in a loop */
  704. *origin_type = NULL;
  705. /* split out the type from the value (use the first ':') */
  706. if ((p = strchr(mapping, ':')) == NULL) {
  707. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  708. "mep_parse_mapped_origin_attr: Value for mapped attribute "
  709. "is not in the correct format. (value: \"%s\").\n",
  710. mapping);
  711. ret = 1;
  712. goto bail;
  713. }
  714. /* Ensure the type is not empty. */
  715. if (p == mapping) {
  716. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  717. "mep_parse_mapped_origin_attr: Value for mapped attribute "
  718. "is not in the correct format. The type is missing. "
  719. "(value: \"%s\").\n",
  720. mapping);
  721. ret = 1;
  722. goto bail;
  723. }
  724. /* Terminate the type so we can use it as a string. */
  725. *p = '\0';
  726. /* Advance p to point to the beginning of the value. */
  727. p++;
  728. while (*p == ' ') {
  729. p++;
  730. }
  731. /* Make end point to the last character that we want in the value. */
  732. end = p + strlen(p) - 1;
  733. /* Find the variable that we need to substitute. */
  734. for (; p <= end; p++) {
  735. if (*p == '$') {
  736. if (p == end) {
  737. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  738. "mep_parse_mapped_origin_attr: Invalid mapped "
  739. "attribute value for type \"%s\".\n", mapping);
  740. ret = 1;
  741. goto bail;
  742. }
  743. if (*(p + 1) == '$') {
  744. /* This is an escaped $. Eliminate the escape character
  745. * to prevent if from being a part of the value. */
  746. p++;
  747. memmove(p, p+1, end-(p+1)+1);
  748. *end = '\0';
  749. end--;
  750. } else {
  751. int quoted = 0;
  752. /* We found a variable. Terminate the pre
  753. * string and process the variable. */
  754. *p = '\0';
  755. p++;
  756. /* Check if the variable name is quoted. If it is, we skip past
  757. * the quoting brace to avoid putting it in the mapped value. */
  758. if (*p == '{') {
  759. quoted = 1;
  760. if (p < end) {
  761. p++;
  762. } else {
  763. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  764. "mep_parse_mapped_origin_attr: Invalid mapped "
  765. "attribute value for type \"%s\".\n", mapping);
  766. ret = 1;
  767. goto bail;
  768. }
  769. }
  770. /* We should be pointing at the variable name now. */
  771. var_start = p;
  772. /* Move the pointer to the end of the variable name. We
  773. * stop at the first character that is not legal for use
  774. * in an attribute description. */
  775. while ((p < end) && IS_ATTRDESC_CHAR(*p)) {
  776. p++;
  777. }
  778. /* If the variable is quoted and this is not a closing
  779. * brace, there is a syntax error in the mapping rule. */
  780. if (quoted && (*p != '}')) {
  781. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  782. "mep_parse_mapped_origin_attr: Invalid mapped "
  783. "attribute value for type \"%s\".\n", mapping);
  784. ret = 1;
  785. goto bail;
  786. }
  787. /* Check for a missing variable name. */
  788. if (p == var_start) {
  789. break;
  790. }
  791. if (p == end) {
  792. /* Set the map type. In this case, p could be
  793. * pointing at either the last character of the
  794. * map type, or at the first character after the
  795. * map type. If the character is valid for use
  796. * in an attribute description, we consider it
  797. * to be a part of the map type. */
  798. if (IS_ATTRDESC_CHAR(*p)) {
  799. *origin_type = strndup(var_start, p - var_start + 1);
  800. /* There is no post string. */
  801. } else {
  802. *origin_type = strndup(var_start, p - var_start);
  803. }
  804. } else {
  805. /* Set the map type. In this case, p is pointing
  806. * at the first character after the map type. */
  807. *origin_type = strndup(var_start, p - var_start);
  808. }
  809. /* We only support a single variable, so we're done. */
  810. break;
  811. }
  812. }
  813. }
  814. bail:
  815. if (ret != 0) {
  816. slapi_ch_free_string(origin_type);
  817. }
  818. return ret;
  819. }
  820. /*
  821. * mep_extract_origin_attrs
  822. *
  823. * Extract the attributes from the template that reside on
  824. * the origin entry that trigger updates to the managed entry.
  825. */
  826. static char **
  827. mep_extract_origin_attrs(Slapi_Entry *template)
  828. {
  829. char **vals = NULL;
  830. char *origin_type = NULL;
  831. char **origin_attrs = NULL;
  832. int i;
  833. if (template) {
  834. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  835. for (i = 0; vals && vals[i]; ++i) {
  836. if (mep_parse_mapped_origin_attr(vals[i], &origin_type) == 0) {
  837. slapi_ch_array_add(&origin_attrs,origin_type);
  838. }
  839. }
  840. slapi_ch_array_free(vals);
  841. }
  842. return origin_attrs;
  843. }
  844. /*
  845. * mep_is_mapped_origin_attr()
  846. *
  847. * Checks if type is a mapped origin attribute.
  848. */
  849. static int
  850. mep_is_mapped_origin_attr(char **vals, char *type)
  851. {
  852. int ret = 0;
  853. int i;
  854. if (type) {
  855. for (i = 0; vals && vals[i]; ++i) {
  856. if (slapi_attr_type_cmp(vals[i], type, SLAPI_TYPE_CMP_EXACT) == 0) {
  857. /* Ok, we are modifying a attribute that affects the managed entry */
  858. ret = 1;
  859. break;
  860. }
  861. }
  862. }
  863. return ret;
  864. }
  865. static Slapi_DN *
  866. mep_get_sdn(Slapi_PBlock * pb)
  867. {
  868. Slapi_DN *sdn = 0;
  869. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  870. "--> mep_get_sdn\n");
  871. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  872. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  873. "<-- mep_get_sdn\n");
  874. return sdn;
  875. }
  876. static void
  877. mep_set_config_area(Slapi_DN *sdn)
  878. {
  879. _ConfigAreaDN = sdn;
  880. }
  881. static Slapi_DN *
  882. mep_get_config_area()
  883. {
  884. return _ConfigAreaDN;
  885. }
  886. /*
  887. * mep_dn_is_config()
  888. *
  889. * Checks if dn is a managed entries config entry.
  890. */
  891. static int
  892. mep_dn_is_config(Slapi_DN *sdn)
  893. {
  894. int ret = 0;
  895. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  896. "--> mep_dn_is_config\n");
  897. if (sdn == NULL) {
  898. goto bail;
  899. }
  900. /* If an alternate config area is configured, treat it's child
  901. * entries as config entries. If the alternate config area is
  902. * not configured, treat children of the top-level plug-in
  903. * config entry as our config entries. */
  904. if (mep_get_config_area()) {
  905. if (slapi_sdn_issuffix(sdn, mep_get_config_area()) &&
  906. slapi_sdn_compare(sdn, mep_get_config_area())) {
  907. ret = 1;
  908. }
  909. } else {
  910. if (slapi_sdn_issuffix(sdn, mep_get_plugin_sdn()) &&
  911. slapi_sdn_compare(sdn, mep_get_plugin_sdn())) {
  912. ret = 1;
  913. }
  914. }
  915. bail:
  916. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  917. "<-- mep_dn_is_config\n");
  918. return ret;
  919. }
  920. /*
  921. * mep_dn_is_template()
  922. *
  923. * Checks if dn is a managed entries template.
  924. */
  925. static int
  926. mep_dn_is_template(Slapi_DN *sdn)
  927. {
  928. int ret = 0;
  929. PRCList *list = NULL;
  930. Slapi_DN *config_sdn = NULL;
  931. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  932. list = PR_LIST_HEAD(g_mep_config);
  933. while (list != g_mep_config) {
  934. config_sdn = ((struct configEntry *)list)->template_sdn;
  935. if (slapi_sdn_compare(config_sdn, sdn) == 0) {
  936. ret = 1;
  937. break;
  938. } else {
  939. list = PR_NEXT_LINK(list);
  940. }
  941. }
  942. }
  943. return ret;
  944. }
  945. /*
  946. * mep_find_config()
  947. *
  948. * Finds the appropriate config entry for a given entry
  949. * by checking if the entry meets the origin scope and
  950. * filter requirements of any config entry. A read lock
  951. * must be held on the config before calling this function.
  952. * The configEntry that is returned is a pointer to the
  953. * actual config entry in the config cache. It should not
  954. * be modified in any way. The read lock should not be
  955. * released until you are finished with the config entry
  956. * that is returned.
  957. *
  958. * Returns NULL if no applicable config entry is found.
  959. */
  960. static void
  961. mep_find_config(Slapi_Entry *e, struct configEntry **config)
  962. {
  963. PRCList *list = NULL;
  964. char *dn = NULL;
  965. *config = NULL;
  966. if (e && !PR_CLIST_IS_EMPTY(g_mep_config)) {
  967. dn = slapi_entry_get_dn(e);
  968. list = PR_LIST_HEAD(g_mep_config);
  969. while (list != g_mep_config) {
  970. /* See if the dn is within the scope of this config entry
  971. * in addition to matching the origin filter. */
  972. if (slapi_dn_issuffix(dn, ((struct configEntry *)list)->origin_scope) &&
  973. (slapi_filter_test_simple(e, ((struct configEntry *)list)->origin_filter) == 0)) {
  974. *config = (struct configEntry *)list;
  975. break;
  976. }
  977. list = PR_NEXT_LINK(list);
  978. }
  979. }
  980. }
  981. /*
  982. * mep_find_config_by_template_dn()
  983. *
  984. * Finds the config entry associated with a particular
  985. * template dn. A read lock must be held on the config
  986. * before calling this function. The configEntry that
  987. * us returned is a pointer to the actual config entry
  988. * in the config cache. It should not be modified in
  989. * any way. The read lock should not be released until
  990. * you are finished with the config entry that is returned.
  991. *
  992. * Returns NULL if no applicable config entry is found.
  993. */
  994. static void
  995. mep_find_config_by_template_dn(Slapi_DN *template_sdn,
  996. struct configEntry **config)
  997. {
  998. PRCList *list = NULL;
  999. Slapi_DN *config_sdn = NULL;
  1000. *config = NULL;
  1001. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  1002. list = PR_LIST_HEAD(g_mep_config);
  1003. while (list != g_mep_config) {
  1004. config_sdn = ((struct configEntry *)list)->template_sdn;
  1005. if (slapi_sdn_compare(config_sdn, template_sdn) == 0) {
  1006. *config = (struct configEntry *)list;
  1007. break;
  1008. } else {
  1009. list = PR_NEXT_LINK(list);
  1010. }
  1011. }
  1012. }
  1013. }
  1014. /*
  1015. * mep_oktodo()
  1016. *
  1017. * Check if we want to process this operation. We need to be
  1018. * sure that the operation succeeded.
  1019. */
  1020. static int
  1021. mep_oktodo(Slapi_PBlock *pb)
  1022. {
  1023. int ret = 1;
  1024. int oprc = 0;
  1025. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1026. "--> mep_oktodo\n" );
  1027. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0) {
  1028. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1029. "mep_oktodo: could not get parameters\n" );
  1030. ret = -1;
  1031. }
  1032. /* This plugin should only execute if the operation succeeded. */
  1033. if(oprc != 0) {
  1034. ret = 0;
  1035. }
  1036. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1037. "<-- mep_oktodo\n" );
  1038. return ret;
  1039. }
  1040. /*
  1041. * mep_isrepl()
  1042. *
  1043. * Returns 1 if the operation associated with pb
  1044. * is a replicated op. Returns 0 otherwise.
  1045. */
  1046. static int
  1047. mep_isrepl(Slapi_PBlock *pb)
  1048. {
  1049. int is_repl = 0;
  1050. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1051. "--> mep_isrepl\n" );
  1052. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  1053. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1054. "<-- mep_isrepl\n" );
  1055. return is_repl;
  1056. }
  1057. /*
  1058. * mep_create_managed_entry()
  1059. *
  1060. * Creates a managed entry from a specified template and origin
  1061. * entry. If an origin entry is not passed in, the values of
  1062. * mapped attributes will not be filled in and the addition of
  1063. * a backlink to the origin is not added. This is useful for
  1064. * creating a test managed entry for template validation.
  1065. */
  1066. static Slapi_Entry *
  1067. mep_create_managed_entry(struct configEntry *config, Slapi_Entry *origin)
  1068. {
  1069. Slapi_Entry *managed_entry = NULL;
  1070. Slapi_Entry *template = NULL;
  1071. char *rdn_type = NULL;
  1072. char **vals = NULL;
  1073. char *type = NULL;
  1074. char *value = NULL;
  1075. Slapi_Value *sval = NULL;
  1076. int found_rdn_map = 0;
  1077. int i = 0;
  1078. int err = 0;
  1079. /* If no template was supplied, there's nothing we can do. */
  1080. if ((config == NULL) || (config->template_entry == NULL)) {
  1081. return NULL;
  1082. } else {
  1083. template = config->template_entry;
  1084. }
  1085. /* Ensure that a RDN type was specified in the template. */
  1086. if ((rdn_type = slapi_entry_attr_get_charptr(template, MEP_RDN_ATTR_TYPE)) == NULL) {
  1087. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1088. "mep_create_managed_entry: The %s config attribute "
  1089. "was not found in template \"%s\". This attribute "
  1090. "is required.\n", MEP_RDN_ATTR_TYPE, slapi_sdn_get_dn(config->template_sdn));
  1091. err = 1;
  1092. goto done;
  1093. }
  1094. /* Create the entry to be returned. */
  1095. managed_entry = slapi_entry_alloc();
  1096. slapi_entry_init(managed_entry, NULL, NULL);
  1097. /* Add all of the static attributes from the template to the newly
  1098. * created managed entry. */
  1099. vals = slapi_entry_attr_get_charray(template, MEP_STATIC_ATTR_TYPE);
  1100. for (i = 0; vals && vals[i]; ++i) {
  1101. struct berval bvtype = {0, NULL}, bvvalue = {0, NULL};
  1102. int freeval = 0;
  1103. if (slapi_ldif_parse_line(vals[i], &bvtype, &bvvalue, &freeval) != 0) {
  1104. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1105. "mep_create_managed_entry: Value for %s config setting "
  1106. "is not in the correct format in template \"%s\". "
  1107. "(value: \"%s\")\n", MEP_STATIC_ATTR_TYPE,
  1108. slapi_sdn_get_dn(config->template_sdn), vals[i]);
  1109. err = 1;
  1110. goto done;
  1111. } else {
  1112. /* Create a berval and add the value to the entry. */
  1113. sval = slapi_value_new_berval(&bvvalue);
  1114. slapi_entry_add_value(managed_entry, bvtype.bv_val, sval);
  1115. slapi_value_free(&sval);
  1116. /* Set type and value to NULL so they don't get
  1117. * free'd by mep_parse_mapped_attr(). */
  1118. if (freeval) {
  1119. slapi_ch_free_string(&bvvalue.bv_val);
  1120. }
  1121. type = NULL;
  1122. value = NULL;
  1123. }
  1124. }
  1125. /* Clear out vals so we can use them again */
  1126. slapi_ch_array_free(vals);
  1127. /* Add the mapped attributes to the newly created managed entry. */
  1128. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  1129. for (i = 0; vals && vals[i]; ++i) {
  1130. if (mep_parse_mapped_attr(vals[i], origin, &type, &value) == 0) {
  1131. /* Add the attribute to the managed entry. */
  1132. slapi_entry_add_string(managed_entry, type, value);
  1133. /* Check if this type is the RDN type. */
  1134. if (slapi_attr_type_cmp(type, rdn_type, SLAPI_TYPE_CMP_EXACT) == 0) {
  1135. found_rdn_map = 1;
  1136. }
  1137. slapi_ch_free_string(&type);
  1138. slapi_ch_free_string(&value);
  1139. } else {
  1140. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1141. "mep_create_managed_entry: Error parsing mapped attribute "
  1142. "in template \"%s\".\n", slapi_sdn_get_dn(config->template_sdn));
  1143. err = 1;
  1144. goto done;
  1145. }
  1146. }
  1147. /* The RDN attribute must be a mapped attribute. If we didn't find it,
  1148. * we need to bail. */
  1149. if (!found_rdn_map) {
  1150. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1151. "mep_create_managed_entry: The RDN type \"%s\" "
  1152. "was not found as a mapped attribute in template "
  1153. "\"%s\". It must be a mapped attribute.\n",
  1154. rdn_type, slapi_sdn_get_dn(config->template_sdn));
  1155. err = 1;
  1156. goto done;
  1157. } else {
  1158. /* Build the DN and set it in the entry. */
  1159. char *dn = NULL;
  1160. char *rdn_val = NULL;
  1161. /* If an origin entry was supplied, the RDN value will be
  1162. * the mapped value. If no origin entry was supplied, the
  1163. * value will be the mapping rule from the template. */
  1164. rdn_val = slapi_entry_attr_get_charptr(managed_entry, rdn_type);
  1165. /* Create the DN using the mapped RDN value
  1166. * and the base specified in the config. */
  1167. dn = slapi_ch_smprintf("%s=%s,%s", rdn_type, rdn_val, config->managed_base);
  1168. slapi_ch_free_string(&rdn_val);
  1169. if (dn != NULL) {
  1170. slapi_sdn_set_dn_passin(slapi_entry_get_sdn(managed_entry), dn);
  1171. } else {
  1172. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1173. "mep_create_managed_entry: Error setting DN "
  1174. "in managed entry based off of template entry "
  1175. "\"%s\" (origin entry \"%s\").\n",
  1176. slapi_sdn_get_dn(config->template_sdn),
  1177. origin ? slapi_entry_get_dn(origin) : "NULL");
  1178. err = 1;
  1179. goto done;
  1180. }
  1181. }
  1182. /* If an origin entry was supplied, set a backpointer to the
  1183. * origin in the managed entry. */
  1184. if (origin) {
  1185. slapi_entry_add_string(managed_entry,
  1186. SLAPI_ATTR_OBJECTCLASS, MEP_MANAGED_OC);
  1187. slapi_entry_add_string(managed_entry, MEP_MANAGED_BY_TYPE,
  1188. slapi_entry_get_dn(origin));
  1189. }
  1190. done:
  1191. slapi_ch_array_free(vals);
  1192. slapi_ch_free_string(&rdn_type);
  1193. if (err != 0) {
  1194. slapi_entry_free(managed_entry);
  1195. managed_entry = NULL;
  1196. }
  1197. return managed_entry;
  1198. }
  1199. /*
  1200. * mep_add_managed_entry()
  1201. *
  1202. * Creates and adds a managed entry to the database. The
  1203. * origin entry will also be modified to add a link to the
  1204. * newly created managed entry.
  1205. */
  1206. static void
  1207. mep_add_managed_entry(struct configEntry *config,
  1208. Slapi_Entry *origin)
  1209. {
  1210. Slapi_Entry *managed_entry = NULL;
  1211. char *managed_dn = NULL;
  1212. Slapi_PBlock *mod_pb = slapi_pblock_new();
  1213. int result = LDAP_SUCCESS;
  1214. /* Create the managed entry */
  1215. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1216. "mep_add_managed_entry: Creating a managed "
  1217. "entry from origin entry \"%s\" using "
  1218. "config \"%s\".\n", slapi_entry_get_dn(origin),
  1219. slapi_sdn_get_dn(config->sdn));
  1220. managed_entry = mep_create_managed_entry(config, origin);
  1221. if (managed_entry == NULL) {
  1222. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1223. "mep_add_managed_entry: Unable to create a managed "
  1224. "entry from origin entry \"%s\" using config "
  1225. "\"%s\".\n", slapi_entry_get_dn(origin), slapi_sdn_get_dn(config->sdn));
  1226. } else {
  1227. /* Copy the managed entry DN to use when
  1228. * creating the pointer attribute. */
  1229. managed_dn = slapi_ch_strdup(slapi_entry_get_dn(managed_entry));
  1230. /* Add managed entry to db. The entry will be consumed. */
  1231. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1232. "Adding managed entry \"%s\" for origin "
  1233. "entry \"%s\"\n.", managed_dn, slapi_entry_get_dn(origin));
  1234. slapi_add_entry_internal_set_pb(mod_pb, managed_entry, NULL,
  1235. mep_get_plugin_id(), 0);
  1236. slapi_add_internal_pb(mod_pb);
  1237. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1238. if (result != LDAP_SUCCESS) {
  1239. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1240. "mep_add_managed_entry: Unable to add managed "
  1241. "entry \"%s\" for origin entry \"%s\" (%s).\n",
  1242. managed_dn, slapi_entry_get_dn(origin),
  1243. ldap_err2string(result));
  1244. } else {
  1245. /* Add forward link to origin entry. */
  1246. LDAPMod oc_mod;
  1247. LDAPMod pointer_mod;
  1248. LDAPMod *mods[3];
  1249. char *oc_vals[2];
  1250. char *pointer_vals[2];
  1251. /* Clear out the pblock for reuse. */
  1252. slapi_pblock_init(mod_pb);
  1253. /* Add the origin entry objectclass. */
  1254. oc_vals[0] = MEP_ORIGIN_OC;
  1255. oc_vals[1] = 0;
  1256. oc_mod.mod_op = LDAP_MOD_ADD;
  1257. oc_mod.mod_type = SLAPI_ATTR_OBJECTCLASS;
  1258. oc_mod.mod_values = oc_vals;
  1259. /* Add a pointer to the managed entry. */
  1260. pointer_vals[0] = managed_dn;
  1261. pointer_vals[1] = 0;
  1262. pointer_mod.mod_op = LDAP_MOD_ADD;
  1263. pointer_mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  1264. pointer_mod.mod_values = pointer_vals;
  1265. mods[0] = &oc_mod;
  1266. mods[1] = &pointer_mod;
  1267. mods[2] = 0;
  1268. /* Perform the modify operation. */
  1269. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1270. "Adding %s pointer to \"%s\" in entry \"%s\"\n.",
  1271. MEP_MANAGED_ENTRY_TYPE, managed_dn, slapi_entry_get_dn(origin));
  1272. slapi_modify_internal_set_pb_ext(mod_pb,
  1273. slapi_entry_get_sdn(origin),
  1274. mods, 0, 0, mep_get_plugin_id(), 0);
  1275. slapi_modify_internal_pb(mod_pb);
  1276. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1277. if (result != LDAP_SUCCESS) {
  1278. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1279. "mep_add_managed_entry: Unable to add pointer to "
  1280. "managed entry \"%s\" in origin entry \"%s\" "
  1281. "(%s).\n", managed_dn, slapi_entry_get_dn(origin),
  1282. ldap_err2string(result));
  1283. }
  1284. }
  1285. }
  1286. slapi_ch_free_string(&managed_dn);
  1287. slapi_pblock_destroy(mod_pb);
  1288. }
  1289. /* mep_rename_managed_entry()
  1290. *
  1291. * Renames a managed entry and updates the pointer in the
  1292. * origin entry.
  1293. */
  1294. static void
  1295. mep_rename_managed_entry(Slapi_Entry *origin,
  1296. Slapi_DN *new_dn, Slapi_DN *old_dn)
  1297. {
  1298. Slapi_RDN *srdn = slapi_rdn_new();
  1299. Slapi_PBlock *mep_pb = slapi_pblock_new();
  1300. LDAPMod mod;
  1301. LDAPMod *mods[2];
  1302. char *vals[2];
  1303. int result = LDAP_SUCCESS;
  1304. /* Just bail if any of our parameters are NULL. */
  1305. if (origin == NULL || new_dn == NULL || old_dn == NULL) {
  1306. goto bail;
  1307. }
  1308. /* Create new RDN */
  1309. slapi_rdn_set_dn(srdn, slapi_sdn_get_ndn(new_dn));
  1310. /* Rename the managed entry. */
  1311. slapi_rename_internal_set_pb_ext(mep_pb, old_dn,
  1312. slapi_rdn_get_rdn(srdn),
  1313. NULL, 1, NULL, NULL, mep_get_plugin_id(), 0);
  1314. slapi_modrdn_internal_pb(mep_pb);
  1315. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1316. if (result != LDAP_SUCCESS) {
  1317. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1318. "mep_rename_managed_entry: Unable to rename managed "
  1319. "entry \"%s\" to \"%s\" (%s).\n", slapi_sdn_get_dn(old_dn),
  1320. slapi_sdn_get_dn(new_dn), ldap_err2string(result));
  1321. } else {
  1322. /* Clear out the pblock for reuse. */
  1323. slapi_pblock_init(mep_pb);
  1324. /* Update the link to the managed entry in the origin entry. */
  1325. vals[0] = (char *)slapi_sdn_get_dn(new_dn);
  1326. vals[1] = 0;
  1327. mod.mod_op = LDAP_MOD_REPLACE;
  1328. mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  1329. mod.mod_values = vals;
  1330. mods[0] = &mod;
  1331. mods[1] = 0;
  1332. /* Perform the modify operation. */
  1333. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1334. "mep_rename_managed_entry: Updating %s pointer to "
  1335. "\"%s\" in entry \"%s\"\n.", MEP_MANAGED_ENTRY_TYPE,
  1336. vals[0], slapi_entry_get_dn(origin));
  1337. slapi_modify_internal_set_pb_ext(mep_pb, slapi_entry_get_sdn(origin),
  1338. mods, 0, 0, mep_get_plugin_id(), 0);
  1339. slapi_modify_internal_pb(mep_pb);
  1340. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1341. if (result != LDAP_SUCCESS) {
  1342. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1343. "mep_rename_managed_entry: Unable to update %s "
  1344. "pointer in entry \"%s\" (%s).\n", MEP_MANAGED_ENTRY_TYPE,
  1345. slapi_entry_get_dn(origin), ldap_err2string(result));
  1346. }
  1347. }
  1348. bail:
  1349. slapi_rdn_free(&srdn);
  1350. slapi_pblock_destroy(mep_pb);
  1351. }
  1352. /*
  1353. * mep_get_mapped_mods()
  1354. *
  1355. * Creates the modifications needed to update the mapped values
  1356. * in a managed entry. It is up to the caller to free the
  1357. * returned mods when it is finished using them.
  1358. */
  1359. static Slapi_Mods *mep_get_mapped_mods(struct configEntry *config,
  1360. Slapi_Entry *origin, char **mapped_dn)
  1361. {
  1362. Slapi_Mods *smods = NULL;
  1363. Slapi_Entry *template = NULL;
  1364. Slapi_Attr *attr = NULL;
  1365. char **vals = NULL;
  1366. char *type = NULL;
  1367. char *value = NULL;
  1368. char *rdn_type = NULL;
  1369. int i = 0;
  1370. /* If no template was supplied, there's nothing we can do. */
  1371. if (origin == NULL || config == NULL || config->template_entry == NULL) {
  1372. return NULL;
  1373. } else {
  1374. template = config->template_entry;
  1375. }
  1376. /* See how many mods we will have and initialize the smods. */
  1377. if (slapi_entry_attr_find(config->template_entry, MEP_MAPPED_ATTR_TYPE, &attr) == 0) {
  1378. int numvals = 0;
  1379. slapi_attr_get_numvalues(attr, &numvals);
  1380. smods = slapi_mods_new();
  1381. slapi_mods_init(smods, numvals);
  1382. }
  1383. /* Find the the RDN type for the managed entry. */
  1384. if ((rdn_type = slapi_entry_attr_get_charptr(template, MEP_RDN_ATTR_TYPE)) == NULL) {
  1385. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1386. "mep_get_mapped_mods: Error getting RDN type from tempate "
  1387. "\"%s\".\n", slapi_sdn_get_dn(config->template_sdn));
  1388. slapi_mods_free(&smods);
  1389. goto bail;
  1390. }
  1391. /* Go through the template and find the mapped attrs. */
  1392. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  1393. for (i = 0; vals && vals[i]; ++i) {
  1394. if (mep_parse_mapped_attr(vals[i], origin, &type, &value) == 0) {
  1395. /* Don't attempt to modify the RDN type, but create
  1396. * the mapped DN if requested. It is up to the caller
  1397. * to free the returned DN. */
  1398. if (slapi_attr_type_cmp(type, rdn_type, SLAPI_TYPE_CMP_EXACT) == 0) {
  1399. if (mapped_dn) {
  1400. *mapped_dn = slapi_create_dn_string("%s=%s,%s", rdn_type,
  1401. value, config->managed_base);
  1402. }
  1403. } else {
  1404. /* Add a modify that replaces all values with the new value. */
  1405. slapi_mods_add_string(smods, LDAP_MOD_REPLACE, type, value);
  1406. }
  1407. slapi_ch_free_string(&type);
  1408. slapi_ch_free_string(&value);
  1409. } else {
  1410. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1411. "mep_get_mapped_mods: Error parsing mapped attribute "
  1412. "in template \"%s\".\n", slapi_sdn_get_dn(config->template_sdn));
  1413. slapi_mods_free(&smods);
  1414. goto bail;
  1415. }
  1416. }
  1417. bail:
  1418. slapi_ch_free_string(&rdn_type);
  1419. slapi_ch_array_free(vals);
  1420. return smods;
  1421. }
  1422. /*
  1423. * mep_parse_mapped_attr()
  1424. *
  1425. * Parses a mapped attribute setting from a template and
  1426. * fills in the type and value based off of the origin
  1427. * entry. If an origin entry is not supplied, the value
  1428. * is simply the mapping rule.
  1429. */
  1430. static int
  1431. mep_parse_mapped_attr(char *mapping, Slapi_Entry *origin,
  1432. char **type, char **value)
  1433. {
  1434. int ret = 0;
  1435. char *p = NULL;
  1436. char *pre_str = NULL;
  1437. char *post_str = NULL;
  1438. char *end = NULL;
  1439. char *var_start = NULL;
  1440. char *map_type = NULL;
  1441. /* Clear out any existing type or value. */
  1442. slapi_ch_free_string(type);
  1443. slapi_ch_free_string(value);
  1444. /* split out the type from the value (use the first ':') */
  1445. if ((p = strchr(mapping, ':')) == NULL) {
  1446. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1447. "mep_parse_mapped_attr: Value for mapped attribute "
  1448. "is not in the correct format. (value: \"%s\").\n",
  1449. mapping);
  1450. ret = 1;
  1451. goto bail;
  1452. }
  1453. /* Ensure the type is not empty. */
  1454. if (p == mapping) {
  1455. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1456. "mep_parse_mapped_attr: Value for mapped attribute "
  1457. "is not in the correct format. The type is missing. "
  1458. "(value: \"%s\").\n",
  1459. mapping);
  1460. ret = 1;
  1461. goto bail;
  1462. }
  1463. /* Terminate the type so we can use it as a string. */
  1464. *p = '\0';
  1465. /* Duplicate the type to be returned. */
  1466. *type = slapi_ch_strdup(mapping);
  1467. /* Advance p to point to the beginning of the value. */
  1468. p++;
  1469. while (*p == ' ') {
  1470. p++;
  1471. }
  1472. pre_str = p;
  1473. /* Make end point to the last character that we want in the value. */
  1474. end = p + strlen(p) - 1;
  1475. /* Find the variable that we need to substitute. */
  1476. for (; p <= end; p++) {
  1477. if (*p == '$') {
  1478. if (p == end) {
  1479. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1480. "mep_parse_mapped_attr: Invalid mapped "
  1481. "attribute value for type \"%s\".\n", mapping);
  1482. ret = 1;
  1483. goto bail;
  1484. }
  1485. if (*(p + 1) == '$') {
  1486. /* This is an escaped $. Eliminate the escape character
  1487. * to prevent if from being a part of the value. */
  1488. p++;
  1489. memmove(p, p+1, end-(p+1)+1);
  1490. *end = '\0';
  1491. end--;
  1492. } else {
  1493. int quoted = 0;
  1494. /* We found a variable. Terminate the pre
  1495. * string and process the variable. */
  1496. *p = '\0';
  1497. p++;
  1498. /* Check if the variable name is quoted. If it is, we skip past
  1499. * the quoting brace to avoid putting it in the mapped value. */
  1500. if (*p == '{') {
  1501. quoted = 1;
  1502. if (p < end) {
  1503. p++;
  1504. } else {
  1505. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1506. "mep_parse_mapped_attr: Invalid mapped "
  1507. "attribute value for type \"%s\".\n", mapping);
  1508. ret = 1;
  1509. goto bail;
  1510. }
  1511. }
  1512. /* We should be pointing at the variable name now. */
  1513. var_start = p;
  1514. /* Move the pointer to the end of the variable name. We
  1515. * stop at the first character that is not legal for use
  1516. * in an attribute description. */
  1517. while ((p < end) && IS_ATTRDESC_CHAR(*p)) {
  1518. p++;
  1519. }
  1520. /* If the variable is quoted and this is not a closing
  1521. * brace, there is a syntax error in the mapping rule. */
  1522. if (quoted && (*p != '}')) {
  1523. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1524. "mep_parse_mapped_attr: Invalid mapped "
  1525. "attribute value for type \"%s\".\n", mapping);
  1526. ret = 1;
  1527. goto bail;
  1528. }
  1529. /* Check for a missing variable name. */
  1530. if (p == var_start) {
  1531. break;
  1532. }
  1533. if (p == end) {
  1534. /* Set the map type. In this case, p could be
  1535. * pointing at either the last character of the
  1536. * map type, or at the first character after the
  1537. * map type. If the character is valid for use
  1538. * in an attribute description, we consider it
  1539. * to be a part of the map type. */
  1540. if (IS_ATTRDESC_CHAR(*p)) {
  1541. map_type = strndup(var_start, p - var_start + 1);
  1542. /* There is no post string. */
  1543. post_str = NULL;
  1544. } else {
  1545. map_type = strndup(var_start, p - var_start);
  1546. post_str = p;
  1547. }
  1548. } else {
  1549. /* Set the map type. In this case, p is pointing
  1550. * at the first character after the map type. */
  1551. map_type = strndup(var_start, p - var_start);
  1552. /* If the variable is quoted, don't include
  1553. * the closing brace in the post string. */
  1554. if (quoted) {
  1555. post_str = p+1;
  1556. } else {
  1557. post_str = p;
  1558. }
  1559. }
  1560. /* Process the post string to remove any escapes. */
  1561. for (p = post_str; p && (p <= end); p++) {
  1562. if (*p == '$') {
  1563. if ((p == end) || (*(p+1) != '$')) {
  1564. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1565. "mep_parse_mapped_attr: Invalid mapped "
  1566. "attribute value for type \"%s\".\n", mapping);
  1567. ret = 1;
  1568. goto bail;
  1569. } else {
  1570. /* This is an escaped '$'. Remove the escape char. */
  1571. p++;
  1572. memmove(p, p+1, end-(p+1)+1);
  1573. *end = '\0';
  1574. end--;
  1575. }
  1576. }
  1577. }
  1578. /* We only support a single variable, so we're done. */
  1579. break;
  1580. }
  1581. }
  1582. }
  1583. if (map_type) {
  1584. if (origin) {
  1585. char *map_val = NULL;
  1586. int freeit = 0;
  1587. /* If the map type is dn, fetch the origin dn. */
  1588. if (slapi_attr_type_cmp(map_type, "dn", SLAPI_TYPE_CMP_EXACT) == 0) {
  1589. map_val = slapi_entry_get_ndn(origin);
  1590. } else {
  1591. map_val = slapi_entry_attr_get_charptr(origin, map_type);
  1592. freeit = 1;
  1593. }
  1594. if (map_val) {
  1595. /* Create the new mapped value. */
  1596. *value = slapi_ch_smprintf("%s%s%s", pre_str,
  1597. map_val, post_str ? post_str : "");
  1598. if (freeit) {
  1599. slapi_ch_free_string(&map_val);
  1600. }
  1601. } else {
  1602. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1603. "mep_parse_mapped_attr: Mapped attribute \"%s\" "
  1604. "is not present in origin entry \"%s\". Please "
  1605. "correct template to only map attributes "
  1606. "required by the schema.\n", map_type,
  1607. slapi_entry_get_dn(origin));
  1608. ret = 1;
  1609. goto bail;
  1610. }
  1611. } else {
  1612. /* Just use the mapping since we have no origin entry. */
  1613. *value = slapi_ch_smprintf("%s$%s%s", pre_str, map_type,
  1614. post_str);
  1615. }
  1616. } else {
  1617. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1618. "mep_parse_mapped_attr: No variable found in "
  1619. "mapped attribute value for type \"%s\".\n",
  1620. mapping);
  1621. ret = 1;
  1622. goto bail;
  1623. }
  1624. bail:
  1625. slapi_ch_free_string(&map_type);
  1626. if (ret != 0) {
  1627. slapi_ch_free_string(type);
  1628. slapi_ch_free_string(value);
  1629. }
  1630. return ret;
  1631. }
  1632. /*
  1633. * mep_is_managed_entry()
  1634. *
  1635. * Returns 1 if the entry is a managed entry, 0 otherwise.
  1636. * The check is performed by seeing if the managed entry
  1637. * objectclass is present.
  1638. */
  1639. static int
  1640. mep_is_managed_entry(Slapi_Entry *e)
  1641. {
  1642. int ret = 0;
  1643. Slapi_Attr *attr = NULL;
  1644. struct berval bv;
  1645. bv.bv_val = MEP_MANAGED_OC;
  1646. bv.bv_len = strlen(bv.bv_val);
  1647. if (e && (slapi_entry_attr_find(e, SLAPI_ATTR_OBJECTCLASS, &attr) == 0)) {
  1648. if (slapi_attr_value_find(attr, &bv) == 0) {
  1649. ret = 1;
  1650. }
  1651. }
  1652. return ret;
  1653. }
  1654. /*
  1655. * mep_is_mapped_attr()
  1656. *
  1657. * Checks if type is defined as a mapped attribute in template.
  1658. */
  1659. static int
  1660. mep_is_mapped_attr(Slapi_Entry *template, char *type)
  1661. {
  1662. int ret = 0;
  1663. int i = 0;
  1664. char **vals = NULL;
  1665. char *map_type = NULL;
  1666. char *value = NULL;
  1667. if (template && type) {
  1668. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  1669. for (i = 0; vals && vals[i]; ++i) {
  1670. if (mep_parse_mapped_attr(vals[i], NULL, &map_type, &value) == 0) {
  1671. if (slapi_attr_type_cmp(map_type, type, SLAPI_TYPE_CMP_EXACT) == 0) {
  1672. ret = 1;
  1673. }
  1674. slapi_ch_free_string(&map_type);
  1675. slapi_ch_free_string(&value);
  1676. /* If we found a match, we're done. */
  1677. if (ret == 1) {
  1678. break;
  1679. }
  1680. }
  1681. }
  1682. slapi_ch_array_free(vals);
  1683. }
  1684. return ret;
  1685. }
  1686. /*
  1687. * Operation callback functions
  1688. */
  1689. /*
  1690. * mep_pre_op()
  1691. *
  1692. * Checks if an operation is modifying the managed
  1693. * entries config and validates the config changes.
  1694. */
  1695. static int
  1696. mep_pre_op(Slapi_PBlock * pb, int modop)
  1697. {
  1698. Slapi_DN *sdn = NULL;
  1699. Slapi_Entry *e = 0;
  1700. Slapi_Mods *smods = 0;
  1701. LDAPMod **mods;
  1702. int free_entry = 0;
  1703. char *errstr = NULL;
  1704. struct configEntry *config = NULL;
  1705. void *caller_id = NULL;
  1706. int ret = SLAPI_PLUGIN_SUCCESS;
  1707. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1708. "--> mep_pre_op\n");
  1709. /* See if we're calling ourselves. */
  1710. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  1711. if (0 == (sdn = mep_get_sdn(pb)))
  1712. goto bail;
  1713. if (mep_dn_is_config(sdn)) {
  1714. /* Validate config changes, but don't apply them.
  1715. * This allows us to reject invalid config changes
  1716. * here at the pre-op stage. Applying the config
  1717. * needs to be done at the post-op stage. */
  1718. if (LDAP_CHANGETYPE_ADD == modop) {
  1719. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  1720. } else if (LDAP_CHANGETYPE_MODIFY == modop) {
  1721. /* Fetch the entry being modified so we can
  1722. * create the resulting entry for validation. */
  1723. if (sdn) {
  1724. slapi_search_internal_get_entry(sdn, 0, &e, mep_get_plugin_id());
  1725. free_entry = 1;
  1726. }
  1727. /* If the entry doesn't exist, just bail and
  1728. * let the server handle it. */
  1729. if (e == NULL) {
  1730. goto bail;
  1731. }
  1732. /* Grab the mods. */
  1733. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1734. smods = slapi_mods_new();
  1735. slapi_mods_init_byref(smods, mods);
  1736. /* Apply the mods to create the resulting entry. */
  1737. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1738. /* The mods don't apply cleanly, so we just let this op go
  1739. * to let the main server handle it. */
  1740. goto bailmod;
  1741. }
  1742. } else if (LDAP_CHANGETYPE_DELETE == modop){
  1743. /* we allow for deletes, so goto bail to we can skip the config parsing */
  1744. goto bail;
  1745. } else {
  1746. /* Refuse other operations. */
  1747. ret = LDAP_UNWILLING_TO_PERFORM;
  1748. errstr = slapi_ch_smprintf("Not a valid operation.");
  1749. goto bail;
  1750. }
  1751. if (mep_parse_config_entry(e, 0) != 0) {
  1752. /* Refuse the operation if config parsing failed. */
  1753. ret = LDAP_UNWILLING_TO_PERFORM;
  1754. if (LDAP_CHANGETYPE_ADD == modop) {
  1755. errstr = slapi_ch_smprintf("Not a valid managed entries configuration entry.");
  1756. } else {
  1757. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1758. "managed entries configuration.");
  1759. }
  1760. }
  1761. } else {
  1762. /* Check if an active template entry is being updated. If so, validate it. */
  1763. mep_config_read_lock();
  1764. /* Bail out if the plug-in close function was just called. */
  1765. if (!slapi_plugin_running(pb)) {
  1766. mep_config_unlock();
  1767. goto bail;
  1768. }
  1769. mep_find_config_by_template_dn(sdn, &config);
  1770. if (config) {
  1771. Slapi_Entry *test_entry = NULL;
  1772. struct configEntry *config_copy = NULL;
  1773. config_copy = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  1774. /* Make a temporary copy of the config to use for validation. */
  1775. config_copy->sdn = slapi_sdn_dup(config->sdn);
  1776. config_copy->managed_base = slapi_ch_strdup(config->managed_base);
  1777. config_copy->template_sdn = slapi_sdn_dup(config->template_sdn);
  1778. /* Reject attempts to delete or rename an active template.
  1779. * Validate changes to an active template. */
  1780. switch (modop) {
  1781. case LDAP_CHANGETYPE_DELETE:
  1782. errstr = slapi_ch_smprintf("Deleting an active managed "
  1783. "entries template is not allowed. "
  1784. "Delete the associated config "
  1785. "entry first.");
  1786. ret = LDAP_UNWILLING_TO_PERFORM;
  1787. break;
  1788. case LDAP_CHANGETYPE_MODDN:
  1789. errstr = slapi_ch_smprintf("Renaming an active managed "
  1790. "entries template is not allowed. "
  1791. "Create a new template and modify "
  1792. "the associated config entry instead.");
  1793. ret = LDAP_UNWILLING_TO_PERFORM;
  1794. break;
  1795. case LDAP_CHANGETYPE_MODIFY:
  1796. /* Fetch the existing template entry. */
  1797. if (sdn) {
  1798. slapi_search_internal_get_entry(sdn, 0, &e, mep_get_plugin_id());
  1799. free_entry = 1;
  1800. }
  1801. /* If the entry doesn't exist, we just skip
  1802. * validation and let the server handle it. */
  1803. if (e) {
  1804. /* Grab the mods. */
  1805. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1806. smods = slapi_mods_new();
  1807. slapi_mods_init_byref(smods, mods);
  1808. /* Apply the mods to create the resulting entry. */
  1809. if (mods && (slapi_entry_apply_mods(e, mods) == LDAP_SUCCESS)) {
  1810. /* Set the resulting template in the config copy.
  1811. * The ownership of the resulting entry is handed
  1812. * over to the config copy. */
  1813. config_copy->template_entry = e;
  1814. e = NULL;
  1815. /* Validate the changed template. */
  1816. test_entry = mep_create_managed_entry(config_copy, NULL);
  1817. if (test_entry == NULL) {
  1818. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1819. "managed entries template.");
  1820. ret = LDAP_UNWILLING_TO_PERFORM;
  1821. } else if (slapi_entry_schema_check(NULL, test_entry) != 0) {
  1822. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1823. "managed entries template due "
  1824. "to a schema violation.");
  1825. ret = LDAP_UNWILLING_TO_PERFORM;
  1826. }
  1827. }
  1828. }
  1829. /* Dispose of the test entry */
  1830. slapi_entry_free(test_entry);
  1831. break;
  1832. }
  1833. /* Free the config copy */
  1834. mep_free_config_entry(&config_copy);
  1835. }
  1836. mep_config_unlock();
  1837. /* If replication, just bail. */
  1838. if (mep_isrepl(pb)) {
  1839. goto bailmod;
  1840. }
  1841. /* Check if a managed entry is being deleted or
  1842. * renamed and reject if it's not being done by
  1843. * this plugin. */
  1844. if (((modop == LDAP_CHANGETYPE_DELETE) || (modop == LDAP_CHANGETYPE_MODDN) ||
  1845. (modop == LDAP_CHANGETYPE_MODIFY)) && (caller_id != mep_get_plugin_id())) {
  1846. Slapi_Entry *origin_e = NULL;
  1847. Slapi_Mod *smod = NULL;
  1848. Slapi_Mod *next_mod = NULL;
  1849. char *origin_dn = NULL;
  1850. Slapi_DN *origin_sdn = NULL;
  1851. /* Fetch the target entry. */
  1852. if (sdn) {
  1853. /* Free any existing entry so we don't leak it. */
  1854. if (e && free_entry) {
  1855. slapi_entry_free(e);
  1856. }
  1857. slapi_search_internal_get_entry(sdn, 0, &e, mep_get_plugin_id());
  1858. free_entry = 1;
  1859. }
  1860. if (e && mep_is_managed_entry(e)) {
  1861. if (modop == LDAP_CHANGETYPE_MODIFY) {
  1862. /* Fetch the origin entry so we can locate the config template. */
  1863. origin_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_BY_TYPE);
  1864. if (origin_dn) {
  1865. origin_sdn = slapi_sdn_new_normdn_byref(origin_dn);
  1866. slapi_search_internal_get_entry(origin_sdn, 0,
  1867. &origin_e, mep_get_plugin_id());
  1868. slapi_sdn_free(&origin_sdn);
  1869. }
  1870. if (origin_e) {
  1871. /* Fetch the config. */
  1872. mep_config_read_lock();
  1873. /* Bail out if the plug-in close function was just called. */
  1874. if (!slapi_plugin_running(pb)) {
  1875. mep_config_unlock();
  1876. goto bail;
  1877. }
  1878. mep_find_config(origin_e, &config);
  1879. if (config) {
  1880. /* Get the mods if we haven't already. */
  1881. if (smods == NULL) {
  1882. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1883. smods = slapi_mods_new();
  1884. slapi_mods_init_byref(smods, mods);
  1885. }
  1886. next_mod = slapi_mod_new();
  1887. smod = slapi_mods_get_first_smod(smods, next_mod);
  1888. while(smod) {
  1889. char *type = (char *)slapi_mod_get_type(smod);
  1890. /* If this is a mapped attribute, reject the op. */
  1891. if (mep_is_mapped_attr(config->template_entry, type)) {
  1892. errstr = slapi_ch_smprintf("Modifying a mapped attribute "
  1893. " in a managed entry is not allowed. "
  1894. "The \"%s\" attribute is mapped for "
  1895. "this entry.", type);
  1896. ret = LDAP_UNWILLING_TO_PERFORM;
  1897. break;
  1898. }
  1899. slapi_mod_done(next_mod);
  1900. smod = slapi_mods_get_next_smod(smods, next_mod);
  1901. }
  1902. slapi_mod_free(&next_mod);
  1903. } else {
  1904. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1905. "mep_pre_op: Unable to fetch config for "
  1906. "origin entry \"%s\".\n", origin_dn);
  1907. }
  1908. slapi_entry_free(origin_e);
  1909. mep_config_unlock();
  1910. } else {
  1911. Slapi_Operation *op;
  1912. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1913. if(operation_is_flag_set(op, OP_FLAG_INTERNAL)){
  1914. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1915. "mep_pre_op: (internal operation) Unable to fetch "
  1916. "origin entry \"%s\".\n", origin_dn);
  1917. } else {
  1918. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1919. "mep_pre_op: Unable to fetch origin entry "
  1920. "\"%s\".\n", origin_dn);
  1921. }
  1922. }
  1923. slapi_ch_free_string(&origin_dn);
  1924. } else {
  1925. errstr = slapi_ch_smprintf("%s a managed entry is not allowed. "
  1926. "It needs to be manually unlinked first.",
  1927. modop == LDAP_CHANGETYPE_DELETE ? "Deleting"
  1928. : "Renaming");
  1929. ret = LDAP_UNWILLING_TO_PERFORM;
  1930. }
  1931. }
  1932. }
  1933. }
  1934. bailmod:
  1935. /* Clean up smods. */
  1936. if (LDAP_CHANGETYPE_MODIFY == modop) {
  1937. slapi_mods_free(&smods);
  1938. }
  1939. bail:
  1940. if (free_entry && e)
  1941. slapi_entry_free(e);
  1942. if (ret) {
  1943. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1944. "mep_pre_op: operation failure [%d]\n", ret);
  1945. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  1946. slapi_ch_free((void **)&errstr);
  1947. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  1948. ret = SLAPI_PLUGIN_FAILURE;
  1949. }
  1950. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1951. "<-- mep_pre_op\n");
  1952. return ret;
  1953. }
  1954. static int
  1955. mep_add_pre_op(Slapi_PBlock * pb)
  1956. {
  1957. return mep_pre_op(pb, LDAP_CHANGETYPE_ADD);
  1958. }
  1959. static int
  1960. mep_del_pre_op(Slapi_PBlock * pb)
  1961. {
  1962. return mep_pre_op(pb, LDAP_CHANGETYPE_DELETE);
  1963. }
  1964. static int
  1965. mep_mod_pre_op(Slapi_PBlock * pb)
  1966. {
  1967. return mep_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  1968. }
  1969. static int
  1970. mep_modrdn_pre_op(Slapi_PBlock * pb)
  1971. {
  1972. return mep_pre_op(pb, LDAP_CHANGETYPE_MODDN);
  1973. }
  1974. static int
  1975. mep_mod_post_op(Slapi_PBlock *pb)
  1976. {
  1977. Slapi_Mods *smods = NULL;
  1978. Slapi_PBlock *mep_pb = NULL;
  1979. Slapi_Entry *e = NULL;
  1980. Slapi_DN *sdn = NULL;
  1981. char *managed_dn = NULL;
  1982. Slapi_DN *managed_sdn = NULL;
  1983. char *mapped_dn = NULL;
  1984. Slapi_DN *mapped_sdn = NULL;
  1985. struct configEntry *config = NULL;
  1986. int result = 0;
  1987. LDAPMod **mods = NULL;
  1988. int i, abort_mod = 1;
  1989. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1990. "--> mep_mod_post_op\n");
  1991. if (mep_oktodo(pb) && (sdn = mep_get_sdn(pb))) {
  1992. /* First check if the config or a template is being modified. */
  1993. if (mep_dn_is_config(sdn) || mep_dn_is_template(sdn)) {
  1994. mep_load_config();
  1995. }
  1996. /* If replication, just bail. */
  1997. if (mep_isrepl(pb)) {
  1998. goto bail;
  1999. }
  2000. /* Fetch the modified entry. This will not be set for a chaining
  2001. * backend, so don't treat the message as fatal. */
  2002. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  2003. if (e == NULL) {
  2004. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2005. "mep_mod_post_op: Unable to fetch postop entry.\n");
  2006. goto bail;
  2007. }
  2008. /* If the entry is a tombstone, just bail. */
  2009. if (mep_has_tombstone_value(e)) {
  2010. goto bail;
  2011. }
  2012. /* Check if we're an origin entry. Update the
  2013. * mapped attrs of it's managed entry if so. */
  2014. managed_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_ENTRY_TYPE);
  2015. if (managed_dn) {
  2016. mep_config_read_lock();
  2017. /* Bail out if the plug-in close function was just called. */
  2018. if (!slapi_plugin_running(pb)) {
  2019. mep_config_unlock();
  2020. goto bail;
  2021. }
  2022. mep_find_config(e, &config);
  2023. if (config) {
  2024. /*
  2025. * Check to see if the applied mods are mapped origin attributes.
  2026. * If they are not, then we don't need to modify the mapped entry
  2027. * as it has not changed.
  2028. */
  2029. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
  2030. for(i = 0; mods && mods[i]; i++){
  2031. if(mep_is_mapped_origin_attr(config->origin_attrs,mods[i]->mod_type)){
  2032. /*
  2033. * We are modifying a managed origin attr, so we can proceed with
  2034. * modifying the managed entry. Otherwise we would modify the
  2035. * managed entry for no reason.
  2036. */
  2037. abort_mod = 0;
  2038. break;
  2039. }
  2040. }
  2041. if(abort_mod){
  2042. mep_config_unlock();
  2043. goto bail;
  2044. }
  2045. smods = mep_get_mapped_mods(config, e, &mapped_dn);
  2046. if (smods) {
  2047. /* Clear out the pblock for reuse. */
  2048. mep_pb = slapi_pblock_new();
  2049. /* Perform the modify operation. */
  2050. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2051. "mep_mod_post_op: Updating mapped attributes "
  2052. "in entry \"%s\"\n.", managed_dn);
  2053. slapi_modify_internal_set_pb(mep_pb, managed_dn,
  2054. slapi_mods_get_ldapmods_byref(smods), 0, 0,
  2055. mep_get_plugin_id(), 0);
  2056. slapi_modify_internal_pb(mep_pb);
  2057. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2058. if (result != LDAP_SUCCESS) {
  2059. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2060. "mep_mod_post_op: Unable to update mapped "
  2061. "attributes from origin entry \"%s\" in managed "
  2062. "entry \"%s\" (%s).\n", slapi_sdn_get_dn(sdn), managed_dn,
  2063. ldap_err2string(result));
  2064. }
  2065. slapi_mods_free(&smods);
  2066. slapi_pblock_destroy(mep_pb);
  2067. }
  2068. /* Check if we need to rename the managed entry. */
  2069. if (mapped_dn) {
  2070. mapped_sdn = slapi_sdn_new_normdn_passin(mapped_dn);
  2071. managed_sdn = slapi_sdn_new_normdn_byref(managed_dn);
  2072. if (slapi_sdn_compare(managed_sdn, mapped_sdn) != 0) {
  2073. mep_rename_managed_entry(e, mapped_sdn, managed_sdn);
  2074. }
  2075. slapi_sdn_free(&mapped_sdn);
  2076. slapi_sdn_free(&managed_sdn);
  2077. }
  2078. } else {
  2079. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2080. "mep_mod_post_op: Unable to find config for origin "
  2081. "entry \"%s\".\n", slapi_sdn_get_dn(sdn));
  2082. }
  2083. mep_config_unlock();
  2084. }
  2085. }
  2086. bail:
  2087. slapi_ch_free_string(&managed_dn);
  2088. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2089. "<-- mep_mod_post_op\n");
  2090. return SLAPI_PLUGIN_SUCCESS;
  2091. }
  2092. static int
  2093. mep_add_post_op(Slapi_PBlock *pb)
  2094. {
  2095. Slapi_Entry *e = NULL;
  2096. Slapi_DN *sdn = NULL;
  2097. struct configEntry *config = NULL;
  2098. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2099. "--> mep_add_post_op\n");
  2100. /* Reload config if a config entry was added. */
  2101. if ((sdn = mep_get_sdn(pb))) {
  2102. if (mep_dn_is_config(sdn)) {
  2103. mep_load_config();
  2104. }
  2105. } else {
  2106. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2107. "mep_add_post_op: Error "
  2108. "retrieving dn\n");
  2109. }
  2110. /* If replication, just bail. */
  2111. if (mep_isrepl(pb)) {
  2112. return SLAPI_PLUGIN_SUCCESS;
  2113. }
  2114. /* Get the newly added entry. */
  2115. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  2116. if (e) {
  2117. /* If the entry is a tombstone, just bail. */
  2118. if (mep_has_tombstone_value(e)) {
  2119. return SLAPI_PLUGIN_SUCCESS;
  2120. }
  2121. /* Check if a config entry applies
  2122. * to the entry being added. */
  2123. mep_config_read_lock();
  2124. /* Bail out if the plug-in close function was just called. */
  2125. if (!slapi_plugin_running(pb)) {
  2126. mep_config_unlock();
  2127. return SLAPI_PLUGIN_SUCCESS;
  2128. }
  2129. mep_find_config(e, &config);
  2130. if (config) {
  2131. mep_add_managed_entry(config, e);
  2132. }
  2133. mep_config_unlock();
  2134. } else {
  2135. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2136. "mep_add_post_op: Error "
  2137. "retrieving post-op entry %s\n", slapi_sdn_get_dn(sdn));
  2138. }
  2139. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2140. "<-- mep_add_post_op\n");
  2141. return SLAPI_PLUGIN_SUCCESS;
  2142. }
  2143. static int
  2144. mep_del_post_op(Slapi_PBlock *pb)
  2145. {
  2146. Slapi_Entry *e = NULL;
  2147. Slapi_DN *sdn = NULL;
  2148. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2149. "--> mep_del_post_op\n");
  2150. /* Reload config if a config entry was deleted. */
  2151. if ((sdn = mep_get_sdn(pb))) {
  2152. if (mep_dn_is_config(sdn))
  2153. mep_load_config();
  2154. } else {
  2155. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2156. "mep_del_post_op: Error "
  2157. "retrieving dn\n");
  2158. }
  2159. /* If replication, just bail. */
  2160. if (mep_isrepl(pb)) {
  2161. return SLAPI_PLUGIN_SUCCESS;
  2162. }
  2163. /* Get deleted entry, then go through types to find config. */
  2164. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &e );
  2165. if (e) {
  2166. char *managed_dn = NULL;
  2167. /* If the entry is a tombstone, just bail. */
  2168. if (mep_has_tombstone_value(e)) {
  2169. return SLAPI_PLUGIN_SUCCESS;
  2170. }
  2171. /* See if we're an origin entry . */
  2172. managed_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_ENTRY_TYPE);
  2173. if (managed_dn) {
  2174. Slapi_PBlock *mep_pb = slapi_pblock_new();
  2175. /* Delete the managed entry. */
  2176. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2177. "mep_del_post_op: Deleting managed entry "
  2178. "\"%s\" due to deletion of origin entry "
  2179. "\"%s\".\n ", managed_dn, slapi_sdn_get_dn(sdn));
  2180. slapi_delete_internal_set_pb(mep_pb, managed_dn, NULL,
  2181. NULL, mep_get_plugin_id(), 0);
  2182. slapi_delete_internal_pb(mep_pb);
  2183. slapi_ch_free_string(&managed_dn);
  2184. slapi_pblock_destroy(mep_pb);
  2185. }
  2186. } else {
  2187. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2188. "mep_del_post_op: Error "
  2189. "retrieving pre-op entry %s\n", slapi_sdn_get_dn(sdn));
  2190. }
  2191. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2192. "<-- mep_del_post_op\n");
  2193. return SLAPI_PLUGIN_SUCCESS;
  2194. }
  2195. static int
  2196. mep_modrdn_post_op(Slapi_PBlock *pb)
  2197. {
  2198. const char *new_dn = NULL;
  2199. Slapi_DN *old_sdn = NULL;
  2200. Slapi_DN *new_sdn = NULL;
  2201. Slapi_Entry *post_e = NULL;
  2202. char *managed_dn = NULL;
  2203. struct configEntry *config = NULL;
  2204. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2205. "--> mep_modrdn_post_op\n");
  2206. /* Just bail if we aren't ready to service requests yet. */
  2207. if (!mep_oktodo(pb)){
  2208. return SLAPI_PLUGIN_SUCCESS;
  2209. }
  2210. /* Reload config if an existing config entry was renamed,
  2211. * or if the new dn brings an entry into the scope of the
  2212. * config entries. */
  2213. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  2214. if (post_e) {
  2215. new_sdn = slapi_entry_get_sdn(post_e);
  2216. new_dn = slapi_sdn_get_dn(new_sdn);
  2217. } else {
  2218. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2219. "mep_modrdn_post_op: Error "
  2220. "retrieving post-op entry\n");
  2221. return SLAPI_PLUGIN_SUCCESS;
  2222. }
  2223. if ((old_sdn = mep_get_sdn(pb))) {
  2224. if (mep_dn_is_config(old_sdn) || mep_dn_is_config(new_sdn))
  2225. mep_load_config();
  2226. } else {
  2227. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2228. "mep_modrdn_post_op: Error "
  2229. "retrieving dn\n");
  2230. }
  2231. /* If replication, just bail. */
  2232. if (mep_isrepl(pb)) {
  2233. return SLAPI_PLUGIN_SUCCESS;
  2234. }
  2235. /* If the entry is a tombstone, just bail. */
  2236. if (mep_has_tombstone_value(post_e)) {
  2237. return SLAPI_PLUGIN_SUCCESS;
  2238. }
  2239. /* See if we're an origin entry . */
  2240. managed_dn = slapi_entry_attr_get_charptr(post_e, MEP_MANAGED_ENTRY_TYPE);
  2241. if (managed_dn) {
  2242. LDAPMod mod;
  2243. LDAPMod *mods[3];
  2244. char *vals[2];
  2245. int result = LDAP_SUCCESS;
  2246. Slapi_PBlock *mep_pb = slapi_pblock_new();
  2247. Slapi_Entry *new_managed_entry = NULL;
  2248. Slapi_DN *managed_sdn = NULL;
  2249. Slapi_Mods *smods = NULL;
  2250. int free_managed_dn = 1;
  2251. mep_config_read_lock();
  2252. /* Bail out if the plug-in close function was just called. */
  2253. if (!slapi_plugin_running(pb)) {
  2254. mep_config_unlock();
  2255. slapi_ch_free_string(&managed_dn);
  2256. slapi_pblock_destroy(mep_pb);
  2257. return SLAPI_PLUGIN_SUCCESS;
  2258. }
  2259. mep_find_config(post_e, &config);
  2260. if (!config) {
  2261. LDAPMod mod2;
  2262. char *vals2[2];
  2263. /* Delete the associated managed entry. */
  2264. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2265. "mep_modrdn_post_op: Removing managed entry \"%s\" "
  2266. "since origin entry \"%s\" was moved out of scope.\n",
  2267. managed_dn, slapi_sdn_get_dn(old_sdn));
  2268. slapi_delete_internal_set_pb (mep_pb, managed_dn, NULL, NULL,
  2269. mep_get_plugin_id(), 0);
  2270. slapi_delete_internal_pb(mep_pb);
  2271. /* Clear out the pblock for reuse. */
  2272. slapi_pblock_init(mep_pb);
  2273. /* Remove the pointer from the origin entry. */
  2274. vals[0] = 0;
  2275. mod.mod_op = LDAP_MOD_DELETE;
  2276. mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  2277. mod.mod_values = vals;
  2278. /* Remove the origin objectclass. */
  2279. vals2[0] = MEP_ORIGIN_OC;
  2280. vals2[1] = 0;
  2281. mod2.mod_op = LDAP_MOD_DELETE;
  2282. mod2.mod_type = SLAPI_ATTR_OBJECTCLASS;
  2283. mod2.mod_values = vals2;
  2284. mods[0] = &mod;
  2285. mods[1] = &mod2;
  2286. mods[2] = 0;
  2287. /* Perform the modify operation. */
  2288. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2289. "mep_modrdn_post_op: Removing %s pointer and %s "
  2290. "objectclass from entry \"%s\".\n",
  2291. MEP_MANAGED_ENTRY_TYPE, MEP_ORIGIN_OC, new_dn);
  2292. slapi_modify_internal_set_pb_ext(mep_pb, new_sdn, mods, 0, 0,
  2293. mep_get_plugin_id(), 0);
  2294. slapi_modify_internal_pb(mep_pb);
  2295. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2296. if (result != LDAP_SUCCESS) {
  2297. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2298. "mep_modrdn_post_op: Unable to remove %s "
  2299. "pointer and %s objectclass from entry "
  2300. "\"%s\".\n", MEP_MANAGED_ENTRY_TYPE,
  2301. MEP_ORIGIN_OC, new_dn);
  2302. }
  2303. } else {
  2304. /* Update backlink to new origin DN in managed entry. */
  2305. vals[0] = (char *)new_dn;
  2306. vals[1] = 0;
  2307. mod.mod_op = LDAP_MOD_REPLACE;
  2308. mod.mod_type = MEP_MANAGED_BY_TYPE;
  2309. mod.mod_values = vals;
  2310. mods[0] = &mod;
  2311. mods[1] = 0;
  2312. /* Create a new managed entry to determine what changes
  2313. * we need to make to the existing managed entry. */
  2314. new_managed_entry = mep_create_managed_entry(config, post_e);
  2315. if (new_managed_entry == NULL) {
  2316. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2317. "mep_modrdn_post_op: Unable to create in-memory "
  2318. "managed entry from origin entry \"%s\".\n", new_dn);
  2319. goto bailmod;
  2320. }
  2321. /* Check if the managed entry exists. It is possible that
  2322. * it has already been renamed by another plug-in. If it
  2323. * has already been renamed, we need to use the new DN to
  2324. * perform our updates. */
  2325. managed_sdn = slapi_sdn_new_normdn_byref(managed_dn);
  2326. if (slapi_search_internal_get_entry(managed_sdn, 0,
  2327. NULL, mep_get_plugin_id()) == LDAP_NO_SUCH_OBJECT) {
  2328. slapi_ch_free_string(&managed_dn);
  2329. /* This DN is not a copy, so we don't want to free it later. */
  2330. managed_dn = slapi_entry_get_dn(new_managed_entry);
  2331. slapi_sdn_set_normdn_byref(managed_sdn, managed_dn);
  2332. free_managed_dn = 0;
  2333. }
  2334. /* Perform the modify operation. */
  2335. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2336. "mep_modrdn_post_op: Updating %s pointer to \"%s\" "
  2337. "in entry \"%s\".\n", MEP_MANAGED_BY_TYPE, new_dn, managed_dn);
  2338. slapi_modify_internal_set_pb(mep_pb, managed_dn, mods, 0, 0,
  2339. mep_get_plugin_id(), 0);
  2340. slapi_modify_internal_pb(mep_pb);
  2341. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2342. if (result != LDAP_SUCCESS) {
  2343. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2344. "mep_modrdn_post_op: Unable to update pointer to "
  2345. "origin entry \"%s\" in managed entry \"%s\" "
  2346. "(%s).\n", new_dn, managed_dn, ldap_err2string(result));
  2347. } else {
  2348. /* See if we need to rename the managed entry. */
  2349. if (slapi_sdn_compare(slapi_entry_get_sdn(new_managed_entry), managed_sdn) != 0) {
  2350. /* Rename the managed entry. */
  2351. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2352. "mep_modrdn_post_op: Renaming managed entry "
  2353. "\"%s\" to \"%s\" due to rename of origin "
  2354. "entry \"%s\".\n ", managed_dn,
  2355. slapi_entry_get_dn(new_managed_entry),
  2356. slapi_sdn_get_dn(old_sdn));
  2357. mep_rename_managed_entry(post_e,
  2358. slapi_entry_get_sdn(new_managed_entry),
  2359. managed_sdn);
  2360. }
  2361. /* Update all of the mapped attributes
  2362. * to be sure they are up to date. */
  2363. smods = mep_get_mapped_mods(config, post_e, NULL);
  2364. if (smods) {
  2365. /* Clear out the pblock for reuse. */
  2366. slapi_pblock_init(mep_pb);
  2367. /* Perform the modify operation. */
  2368. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2369. "mep_modrdn_post_op: Updating mapped attributes "
  2370. "in entry \"%s\"\n.", managed_dn);
  2371. slapi_modify_internal_set_pb_ext(mep_pb,
  2372. slapi_entry_get_sdn(new_managed_entry),
  2373. slapi_mods_get_ldapmods_byref(smods), 0, 0,
  2374. mep_get_plugin_id(), 0);
  2375. slapi_modify_internal_pb(mep_pb);
  2376. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2377. if (result != LDAP_SUCCESS) {
  2378. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2379. "mep_modrdn_post_op: Unable to update mapped "
  2380. "attributes from origin entry \"%s\" in managed "
  2381. "entry \"%s\" (%s).\n", new_dn,
  2382. slapi_entry_get_dn(new_managed_entry),
  2383. ldap_err2string(result));
  2384. }
  2385. slapi_mods_free(&smods);
  2386. }
  2387. }
  2388. bailmod:
  2389. slapi_entry_free(new_managed_entry);
  2390. slapi_sdn_free(&managed_sdn);
  2391. }
  2392. slapi_pblock_destroy(mep_pb);
  2393. if (free_managed_dn) {
  2394. slapi_ch_free_string(&managed_dn);
  2395. }
  2396. mep_config_unlock();
  2397. } else {
  2398. /* Was this entry moved into scope of a config entry?
  2399. * If so, treat like an add and create the new managed
  2400. * entry and links. */
  2401. mep_config_read_lock();
  2402. /* Bail out if the plug-in close function was just called. */
  2403. if (!slapi_plugin_running(pb)) {
  2404. mep_config_unlock();
  2405. return SLAPI_PLUGIN_SUCCESS;
  2406. }
  2407. mep_find_config(post_e, &config);
  2408. if (config) {
  2409. mep_add_managed_entry(config, post_e);
  2410. }
  2411. mep_config_unlock();
  2412. }
  2413. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2414. "<-- mep_modrdn_post_op\n");
  2415. return SLAPI_PLUGIN_SUCCESS;
  2416. }
  2417. static int
  2418. mep_has_tombstone_value(Slapi_Entry * e)
  2419. {
  2420. Slapi_Value *tombstone = slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
  2421. int rc = slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS,
  2422. tombstone);
  2423. slapi_value_free(&tombstone);
  2424. return rc;
  2425. }