mep.c 76 KB

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