mep.c 86 KB

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