mep.c 98 KB

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