mep.c 97 KB

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