1
0

mep.c 90 KB

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