mep.c 88 KB

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