mep.c 76 KB

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