automember.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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) 2011 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * Auto Membership Plug-in
  42. */
  43. #include "automember.h"
  44. /*
  45. * Plug-in globals
  46. */
  47. static PRCList *g_automember_config = NULL;
  48. static PRRWLock *g_automember_config_lock;
  49. static void *_PluginID = NULL;
  50. static Slapi_DN *_PluginDN = NULL;
  51. static Slapi_DN *_ConfigAreaDN = NULL;
  52. static int g_plugin_started = 0;
  53. static Slapi_PluginDesc pdesc = { AUTOMEMBER_FEATURE_DESC,
  54. VENDOR,
  55. DS_PACKAGE_VERSION,
  56. AUTOMEMBER_PLUGIN_DESC };
  57. /*
  58. * Plug-in management functions
  59. */
  60. int automember_init(Slapi_PBlock * pb);
  61. static int automember_start(Slapi_PBlock * pb);
  62. static int automember_close(Slapi_PBlock * pb);
  63. static int automember_postop_init(Slapi_PBlock * pb);
  64. static int automember_internal_postop_init(Slapi_PBlock *pb);
  65. /*
  66. * Operation callbacks (where the real work is done)
  67. */
  68. static int automember_mod_post_op(Slapi_PBlock *pb);
  69. static int automember_add_post_op(Slapi_PBlock *pb);
  70. static int automember_del_post_op(Slapi_PBlock *pb);
  71. static int automember_modrdn_post_op(Slapi_PBlock *pb);
  72. static int automember_pre_op(Slapi_PBlock *pb, int modop);
  73. static int automember_mod_pre_op(Slapi_PBlock *pb);
  74. static int automember_add_pre_op(Slapi_PBlock *pb);
  75. /*
  76. * Config cache management functions
  77. */
  78. static int automember_load_config();
  79. static void automember_delete_config();
  80. static int automember_parse_config_entry(Slapi_Entry * e, int apply);
  81. static void automember_free_config_entry(struct configEntry ** entry);
  82. /*
  83. * helpers
  84. */
  85. static char *automember_get_dn(Slapi_PBlock * pb);
  86. static Slapi_DN *automember_get_config_area();
  87. static void automember_set_config_area(Slapi_DN *sdn);
  88. static int automember_dn_is_config(char *dn);
  89. static int automember_oktodo(Slapi_PBlock *pb);
  90. static int automember_isrepl(Slapi_PBlock *pb);
  91. static struct automemberRegexRule *automember_parse_regex_rule(char *rule_string);
  92. static void automember_free_regex_rule(struct automemberRegexRule *rule);
  93. static int automember_parse_grouping_attr(char *value, char **grouping_attr,
  94. char **grouping_value);
  95. static void automember_update_membership(struct configEntry *config, Slapi_Entry *e);
  96. static void automember_add_member_value(Slapi_Entry *member_e, const char *group_dn,
  97. char *grouping_attr, char *grouping_value);
  98. /*
  99. * Config cache locking functions
  100. */
  101. void
  102. automember_config_read_lock()
  103. {
  104. PR_RWLock_Rlock(g_automember_config_lock);
  105. }
  106. void
  107. automember_config_write_lock()
  108. {
  109. PR_RWLock_Wlock(g_automember_config_lock);
  110. }
  111. void
  112. automember_config_unlock()
  113. {
  114. PR_RWLock_Unlock(g_automember_config_lock);
  115. }
  116. /*
  117. * Plugin identity functions
  118. */
  119. void
  120. automember_set_plugin_id(void *pluginID)
  121. {
  122. _PluginID = pluginID;
  123. }
  124. void *
  125. automember_get_plugin_id()
  126. {
  127. return _PluginID;
  128. }
  129. void
  130. automember_set_plugin_sdn(Slapi_DN *pluginDN)
  131. {
  132. _PluginDN = pluginDN;
  133. }
  134. Slapi_DN *
  135. automember_get_plugin_sdn()
  136. {
  137. return _PluginDN;
  138. }
  139. /*
  140. * Plug-in initialization functions
  141. */
  142. int
  143. automember_init(Slapi_PBlock *pb)
  144. {
  145. int status = 0;
  146. char *plugin_identity = NULL;
  147. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  148. "--> automember_init\n");
  149. /* Store the plugin identity for later use.
  150. * Used for internal operations. */
  151. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  152. PR_ASSERT(plugin_identity);
  153. automember_set_plugin_id(plugin_identity);
  154. /* Register callbacks */
  155. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  156. SLAPI_PLUGIN_VERSION_01) != 0 ||
  157. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  158. (void *) automember_start) != 0 ||
  159. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  160. (void *) automember_close) != 0 ||
  161. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  162. (void *) &pdesc) != 0 ||
  163. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  164. (void *) automember_mod_pre_op) != 0 ||
  165. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  166. (void *) automember_add_pre_op) != 0 ||
  167. slapi_register_plugin("internalpostoperation", /* op type */
  168. 1, /* Enabled */
  169. "automember_init", /* this function desc */
  170. automember_internal_postop_init, /* init func */
  171. AUTOMEMBER_INT_POSTOP_DESC, /* plugin desc */
  172. NULL, /* ? */
  173. plugin_identity /* access control */
  174. ) ||
  175. slapi_register_plugin("postoperation", /* op type */
  176. 1, /* Enabled */
  177. "automember_init", /* this function desc */
  178. automember_postop_init, /* init func for post op */
  179. AUTOMEMBER_POSTOP_DESC, /* plugin desc */
  180. NULL, /* ? */
  181. plugin_identity /* access control */
  182. )
  183. ) {
  184. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  185. "automember_init: failed to register plugin\n");
  186. status = -1;
  187. }
  188. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  189. "<-- automember_init\n");
  190. return status;
  191. }
  192. static int
  193. automember_internal_postop_init(Slapi_PBlock *pb)
  194. {
  195. int status = 0;
  196. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  197. SLAPI_PLUGIN_VERSION_01) != 0 ||
  198. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  199. (void *) &pdesc) != 0 ||
  200. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  201. (void *) automember_add_post_op) != 0 ||
  202. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  203. (void *) automember_del_post_op) != 0 ||
  204. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  205. (void *) automember_mod_post_op) != 0 ||
  206. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  207. (void *) automember_modrdn_post_op) != 0) {
  208. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  209. "automember_internal_postop_init: failed to register plugin\n");
  210. status = -1;
  211. }
  212. return status;
  213. }
  214. static int
  215. automember_postop_init(Slapi_PBlock *pb)
  216. {
  217. int status = 0;
  218. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  219. SLAPI_PLUGIN_VERSION_01) != 0 ||
  220. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  221. (void *) &pdesc) != 0 ||
  222. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,
  223. (void *) automember_add_post_op) != 0 ||
  224. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN,
  225. (void *) automember_del_post_op) != 0 ||
  226. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN,
  227. (void *) automember_mod_post_op) != 0 ||
  228. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN,
  229. (void *) automember_modrdn_post_op) != 0) {
  230. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  231. "automember_postop_init: failed to register plugin\n");
  232. status = -1;
  233. }
  234. return status;
  235. }
  236. /* Stash the config area in the pblock for start functions? */
  237. /*
  238. * automember_start()
  239. *
  240. * Creates config lock and loads config cache.
  241. */
  242. static int
  243. automember_start(Slapi_PBlock * pb)
  244. {
  245. char *plugindn = NULL;
  246. char *config_area = NULL;
  247. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  248. "--> automember_start\n");
  249. /* Check if we're already started */
  250. if (g_plugin_started) {
  251. goto done;
  252. }
  253. g_automember_config_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "automember_config");
  254. if (!g_automember_config_lock) {
  255. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  256. "automember_start: lock creation failed\n");
  257. return -1;
  258. }
  259. /*
  260. * Get the plug-in target dn from the system
  261. * and store it for future use. */
  262. slapi_pblock_get(pb, SLAPI_TARGET_DN, &plugindn);
  263. if (NULL == plugindn || 0 == strlen(plugindn)) {
  264. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  265. "automember_start: unable to retrieve plugin dn\n");
  266. return -1;
  267. }
  268. automember_set_plugin_sdn(slapi_sdn_new_dn_byref(plugindn));
  269. /* Set the alternate config area if one is defined. */
  270. slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
  271. if (config_area) {
  272. automember_set_config_area(slapi_sdn_new_dn_byval(config_area));
  273. }
  274. /*
  275. * Load the config cache
  276. */
  277. g_automember_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  278. PR_INIT_CLIST(g_automember_config);
  279. if (automember_load_config() != 0) {
  280. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  281. "automember_start: unable to load plug-in configuration\n");
  282. return -1;
  283. }
  284. g_plugin_started = 1;
  285. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  286. "auto membership plug-in: ready for service\n");
  287. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  288. "<-- automember_start\n");
  289. done:
  290. return 0;
  291. }
  292. /*
  293. * automember_close()
  294. *
  295. * Cleans up the config cache.
  296. */
  297. static int
  298. automember_close(Slapi_PBlock * pb)
  299. {
  300. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  301. "--> automember_close\n");
  302. if (!g_plugin_started) {
  303. goto done;
  304. }
  305. automember_config_write_lock();
  306. g_plugin_started = 0;
  307. automember_delete_config();
  308. automember_config_unlock();
  309. slapi_ch_free((void **)&g_automember_config);
  310. slapi_sdn_free(&_PluginDN);
  311. slapi_sdn_free(&_ConfigAreaDN);
  312. /* We explicitly don't destroy the config lock here. If we did,
  313. * there is the slight possibility that another thread that just
  314. * passed the g_plugin_started check is about to try to obtain
  315. * a reader lock. We leave the lock around so these threads
  316. * don't crash the process. If we always check the started
  317. * flag again after obtaining a reader lock, no free'd resources
  318. * will be used. */
  319. done:
  320. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  321. "<-- automember_close\n");
  322. return 0;
  323. }
  324. /*
  325. * automember_get_config()
  326. *
  327. * Get the config list.
  328. */
  329. PRCList *
  330. automember_get_config()
  331. {
  332. return g_automember_config;
  333. }
  334. /*
  335. * automember_load_config()
  336. *
  337. * Parse and load the config entries.
  338. */
  339. static int
  340. automember_load_config()
  341. {
  342. int status = 0;
  343. int result;
  344. int i;
  345. Slapi_PBlock *search_pb;
  346. Slapi_Entry **entries = NULL;
  347. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  348. "--> automember_load_config\n");
  349. /* Clear out any old config. */
  350. automember_config_write_lock();
  351. automember_delete_config();
  352. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  353. "automember_load_config: Looking for config entries "
  354. "beneath \"%s\".\n", slapi_sdn_get_ndn(automember_get_plugin_sdn()));
  355. search_pb = slapi_pblock_new();
  356. /* If an alternate config area is configured, find
  357. * the config entries that are beneath it, otherwise
  358. * we load the entries beneath our top-level plug-in
  359. * config entry. */
  360. if (automember_get_config_area()) {
  361. /* Find the config entries beneath the alternate config area. */
  362. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  363. "automember_load_config: Looking for config entries "
  364. "beneath \"%s\".\n", slapi_sdn_get_ndn(automember_get_config_area()));
  365. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(automember_get_config_area()),
  366. LDAP_SCOPE_SUBTREE, "objectclass=*",
  367. NULL, 0, NULL, NULL, automember_get_plugin_id(), 0);
  368. } else {
  369. /* Find the config entries beneath our plugin entry. */
  370. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  371. "automember_load_config: Looking for config entries "
  372. "beneath \"%s\".\n", slapi_sdn_get_ndn(automember_get_plugin_sdn()));
  373. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(automember_get_plugin_sdn()),
  374. LDAP_SCOPE_SUBTREE, "objectclass=*",
  375. NULL, 0, NULL, NULL, automember_get_plugin_id(), 0);
  376. }
  377. slapi_search_internal_pb(search_pb);
  378. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  379. if (LDAP_SUCCESS != result) {
  380. if (automember_get_config_area() && (result == LDAP_NO_SUCH_OBJECT)) {
  381. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  382. "automember_load_config: Config container \"%s\" does "
  383. "not exist.\n", slapi_sdn_get_ndn(automember_get_config_area()));
  384. goto cleanup;
  385. } else {
  386. status = -1;
  387. goto cleanup;
  388. }
  389. }
  390. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  391. &entries);
  392. /* Loop through all of the entries we found and parse them. */
  393. for (i = 0; entries && (entries[i] != NULL); i++) {
  394. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  395. "automember_load_config: parsing config entry "
  396. "\"%s\".\n", slapi_entry_get_dn(entries[i]));
  397. /* We don't care about the status here because we may have
  398. * some invalid config entries, but we just want to continue
  399. * looking for valid ones. */
  400. automember_parse_config_entry(entries[i], 1);
  401. }
  402. cleanup:
  403. slapi_free_search_results_internal(search_pb);
  404. slapi_pblock_destroy(search_pb);
  405. automember_config_unlock();
  406. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  407. "<-- automember_load_config\n");
  408. return status;
  409. }
  410. /*
  411. * automember_parse_config_entry()
  412. *
  413. * Parses a single config entry. If apply is non-zero, then
  414. * we will load and start using the new config. You can simply
  415. * validate config without making any changes by setting apply
  416. * to 0.
  417. *
  418. * Returns 0 if the entry is valid and -1 if it is invalid.
  419. */
  420. static int
  421. automember_parse_config_entry(Slapi_Entry * e, int apply)
  422. {
  423. char *value = NULL;
  424. char **values = NULL;
  425. struct configEntry *entry = NULL;
  426. struct configEntry *config_entry;
  427. PRCList *list;
  428. int entry_added = 0;
  429. int i = 0;
  430. int ret = 0;
  431. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  432. "--> automember_parse_config_entry\n");
  433. /* If this is the main plug-in config entry or
  434. * the config area container, just bail. */
  435. if ((slapi_sdn_compare(automember_get_plugin_sdn(), slapi_entry_get_sdn(e)) == 0) ||
  436. (automember_get_config_area() && (slapi_sdn_compare(automember_get_config_area(),
  437. slapi_entry_get_sdn(e)) == 0))) {
  438. goto bail;
  439. }
  440. /* If marked as disabled, just bail. */
  441. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_DISABLED_TYPE);
  442. if (value) {
  443. slapi_ch_free_string(&value);
  444. goto bail;
  445. }
  446. entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  447. if (NULL == entry) {
  448. ret = -1;
  449. goto bail;
  450. }
  451. value = slapi_entry_get_ndn(e);
  452. if (value) {
  453. entry->dn = slapi_ch_strdup(value);
  454. } else {
  455. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  456. "automember_parse_config_entry: Error "
  457. "reading dn from config entry\n");
  458. ret = -1;
  459. goto bail;
  460. }
  461. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  462. "----------> dn [%s]\n", entry->dn);
  463. /* Load the scope */
  464. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_SCOPE_TYPE);
  465. if (value) {
  466. entry->scope = value;
  467. } else {
  468. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  469. "automember_parse_config_entry: The %s config "
  470. "setting is required for config entry \"%s\".\n",
  471. AUTOMEMBER_SCOPE_TYPE, entry->dn);
  472. ret = -1;
  473. goto bail;
  474. }
  475. /* Load the filter */
  476. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_FILTER_TYPE);
  477. if (value) {
  478. /* Convert to a Slapi_Filter to improve performance. */
  479. if (NULL == (entry->filter = slapi_str2filter(value))) {
  480. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM ,
  481. "automember_parse_config_entry: Invalid search filter in "
  482. "%s config setting for config entry \"%s\" "
  483. "(filter = \"%s\").\n", AUTOMEMBER_FILTER_TYPE, entry->dn, value);
  484. ret = -1;
  485. }
  486. slapi_ch_free_string(&value);
  487. if (ret != 0) {
  488. goto bail;
  489. }
  490. } else {
  491. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  492. "automember_parse_config_entry: The %s config "
  493. "setting is required for config entry \"%s\".\n",
  494. AUTOMEMBER_FILTER_TYPE, entry->dn);
  495. ret = -1;
  496. goto bail;
  497. }
  498. /* Load exclusive regex rules */
  499. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_EXC_REGEX_TYPE);
  500. if (values) {
  501. struct automemberRegexRule *rule = NULL;
  502. /* Create a list to hold our regex rules */
  503. entry->exclusive_rules = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  504. PR_INIT_CLIST((PRCList *)entry->exclusive_rules);
  505. /* Parse each regex rule and add to the list */
  506. for (i = 0; values && values[i]; ++i) {
  507. rule = automember_parse_regex_rule(values[i]);
  508. if (rule) {
  509. if (!PR_CLIST_IS_EMPTY((PRCList *)entry->exclusive_rules)) {
  510. list = PR_LIST_HEAD((PRCList *)entry->exclusive_rules);
  511. while (list != (PRCList *)entry->exclusive_rules) {
  512. struct automemberRegexRule *curr_rule = (struct automemberRegexRule *)list;
  513. /* Order rules by target group DN */
  514. if (slapi_sdn_compare(rule->target_group_dn, curr_rule->target_group_dn) < 0) {
  515. PR_INSERT_BEFORE(&(rule->list), list);
  516. break;
  517. }
  518. list = PR_NEXT_LINK(list);
  519. /* If we hit the end of the list, add to the tail. */
  520. if ((PRCList *)entry->exclusive_rules == list) {
  521. PR_INSERT_BEFORE(&(rule->list), list);
  522. break;
  523. }
  524. }
  525. } else {
  526. /* Add to head of list */
  527. PR_INSERT_LINK(&(rule->list), (PRCList *)entry->exclusive_rules);
  528. }
  529. } else {
  530. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  531. "automember_parse_config_entry: Invalid exclusive "
  532. "regex rule in config entry \"%s\" (rule = \"%s\").\n",
  533. entry->dn, values[i]);
  534. ret = -1;
  535. }
  536. }
  537. slapi_ch_array_free(values);
  538. values = NULL;
  539. /* Bail if we had a bad regex rule */
  540. if (ret == -1) {
  541. goto bail;
  542. }
  543. }
  544. /* Load inclusive regex rules */
  545. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_INC_REGEX_TYPE);
  546. if (values) {
  547. struct automemberRegexRule *rule = NULL;
  548. /* Create a list to hold our regex rules */
  549. entry->inclusive_rules = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  550. PR_INIT_CLIST((PRCList *)entry->inclusive_rules);
  551. /* Parse each regex rule and add to the list */
  552. for (i = 0; values && values[i]; ++i) {
  553. rule = automember_parse_regex_rule(values[i]);
  554. if (rule) {
  555. if (!PR_CLIST_IS_EMPTY((PRCList *)entry->inclusive_rules)) {
  556. list = PR_LIST_HEAD((PRCList *)entry->inclusive_rules);
  557. while (list != (PRCList *)entry->inclusive_rules) {
  558. struct automemberRegexRule *curr_rule = (struct automemberRegexRule *)list;
  559. /* Order rules by target group DN */
  560. if (slapi_sdn_compare(rule->target_group_dn, curr_rule->target_group_dn) < 0) {
  561. PR_INSERT_BEFORE(&(rule->list), list);
  562. break;
  563. }
  564. list = PR_NEXT_LINK(list);
  565. /* If we hit the end of the list, add to the tail. */
  566. if ((PRCList *)entry->inclusive_rules == list) {
  567. PR_INSERT_BEFORE(&(rule->list), list);
  568. break;
  569. }
  570. }
  571. } else {
  572. /* Add to head of list */
  573. PR_INSERT_LINK(&(rule->list), (PRCList *)entry->inclusive_rules);
  574. }
  575. } else {
  576. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  577. "automember_parse_config_entry: Invalid inclusive "
  578. "regex rule in config entry \"%s\" (rule = \"%s\").\n",
  579. entry->dn, values[i]);
  580. ret = -1;
  581. }
  582. }
  583. slapi_ch_array_free(values);
  584. values = NULL;
  585. }
  586. /* Load the default groups */
  587. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_DEFAULT_GROUP_TYPE);
  588. if (values) {
  589. /* Just hand off the values */
  590. entry->default_groups = values;
  591. values = NULL;
  592. }
  593. /* Load the grouping attr */
  594. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_GROUPING_ATTR_TYPE);
  595. if (value) {
  596. if (automember_parse_grouping_attr(value, &(entry->grouping_attr),
  597. &(entry->grouping_value)) != 0) {
  598. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  599. "automember_parse_config_entry: Invalid "
  600. "%s config setting for config entry \"%s\" "
  601. "(value: \"%s\").\n", AUTOMEMBER_GROUPING_ATTR_TYPE,
  602. entry->dn, value);
  603. ret = -1;
  604. }
  605. slapi_ch_free_string(&value);
  606. if (ret != 0) {
  607. goto bail;
  608. }
  609. } else {
  610. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  611. "automember_parse_config_entry: The %s config "
  612. "setting is required for config entry \"%s\".\n",
  613. AUTOMEMBER_GROUPING_ATTR_TYPE, entry->dn);
  614. ret = -1;
  615. goto bail;
  616. }
  617. /* If we were only called to validate config, we can
  618. * just bail out before applying the config changes */
  619. if (apply == 0) {
  620. goto bail;
  621. }
  622. /* Add the config object to the list. We order by scope. */
  623. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  624. list = PR_LIST_HEAD(g_automember_config);
  625. while (list != g_automember_config) {
  626. config_entry = (struct configEntry *) list;
  627. /* If the config entry we are adding has a scope that is
  628. * a child of the scope of the current list item, we insert
  629. * the entry before that list item. */
  630. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  631. PR_INSERT_BEFORE(&(entry->list), list);
  632. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  633. "store [%s] before [%s] \n", entry->dn,
  634. config_entry->dn);
  635. entry_added = 1;
  636. break;
  637. }
  638. list = PR_NEXT_LINK(list);
  639. /* If we hit the end of the list, add to the tail. */
  640. if (g_automember_config == list) {
  641. PR_INSERT_BEFORE(&(entry->list), list);
  642. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  643. "store [%s] at tail\n", entry->dn);
  644. entry_added = 1;
  645. break;
  646. }
  647. }
  648. } else {
  649. /* first entry */
  650. PR_INSERT_LINK(&(entry->list), g_automember_config);
  651. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  652. "store [%s] at head \n", entry->dn);
  653. entry_added = 1;
  654. }
  655. bail:
  656. if (0 == entry_added) {
  657. /* Don't log error if we weren't asked to apply config */
  658. if ((apply != 0) && (entry != NULL)) {
  659. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  660. "automember_parse_config_entry: Invalid config entry "
  661. "[%s] skipped\n", entry->dn);
  662. }
  663. automember_free_config_entry(&entry);
  664. } else {
  665. ret = 0;
  666. }
  667. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  668. "<-- automember_parse_config_entry\n");
  669. return ret;
  670. }
  671. static void
  672. automember_free_config_entry(struct configEntry ** entry)
  673. {
  674. struct configEntry *e = *entry;
  675. if (e == NULL)
  676. return;
  677. if (e->dn) {
  678. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  679. "freeing config entry [%s]\n", e->dn);
  680. slapi_ch_free_string(&e->dn);
  681. }
  682. if (e->scope) {
  683. slapi_ch_free_string(&e->scope);
  684. }
  685. if (e->filter) {
  686. slapi_filter_free(e->filter, 1);
  687. }
  688. if (e->exclusive_rules) {
  689. PRCList *list;
  690. /* Clear out the list contents. */
  691. while (!PR_CLIST_IS_EMPTY((PRCList *)e->exclusive_rules)) {
  692. list = PR_LIST_HEAD((PRCList *)e->exclusive_rules);
  693. PR_REMOVE_LINK(list);
  694. automember_free_regex_rule((struct automemberRegexRule *)list);
  695. }
  696. /* Free the list itself. */
  697. slapi_ch_free((void **)&(e->exclusive_rules));
  698. }
  699. /* Clear out the list contents. */
  700. if (e->inclusive_rules) {
  701. PRCList *list;
  702. while (!PR_CLIST_IS_EMPTY((PRCList *)e->inclusive_rules)) {
  703. list = PR_LIST_HEAD((PRCList *)e->inclusive_rules);
  704. PR_REMOVE_LINK(list);
  705. automember_free_regex_rule((struct automemberRegexRule *)list);
  706. }
  707. /* Free the list itself. */
  708. slapi_ch_free((void **)&(e->inclusive_rules));
  709. }
  710. if (e->default_groups) {
  711. slapi_ch_array_free(e->default_groups);
  712. }
  713. if (e->grouping_attr) {
  714. slapi_ch_free_string(&e->grouping_attr);
  715. }
  716. if (e->grouping_value) {
  717. slapi_ch_free_string(&e->grouping_value);
  718. }
  719. slapi_ch_free((void **)entry);
  720. }
  721. static void
  722. automember_delete_configEntry(PRCList *entry)
  723. {
  724. PR_REMOVE_LINK(entry);
  725. automember_free_config_entry((struct configEntry **) &entry);
  726. }
  727. static void
  728. automember_delete_config()
  729. {
  730. PRCList *list;
  731. /* Delete the config cache. */
  732. while (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  733. list = PR_LIST_HEAD(g_automember_config);
  734. automember_delete_configEntry(list);
  735. }
  736. return;
  737. }
  738. /*
  739. * Helper functions
  740. */
  741. static char *
  742. automember_get_dn(Slapi_PBlock * pb)
  743. {
  744. char *dn = 0;
  745. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  746. "--> automember_get_dn\n");
  747. if (slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn)) {
  748. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  749. "automember_get_dn: failed to get dn of changed entry");
  750. goto bail;
  751. }
  752. bail:
  753. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  754. "<-- automember_get_dn\n");
  755. return dn;
  756. }
  757. void
  758. automember_set_config_area(Slapi_DN *sdn)
  759. {
  760. _ConfigAreaDN = sdn;
  761. }
  762. Slapi_DN *
  763. automember_get_config_area()
  764. {
  765. return _ConfigAreaDN;
  766. }
  767. /*
  768. * automember_dn_is_config()
  769. *
  770. * Checks if dn is an auto membership config entry.
  771. */
  772. static int
  773. automember_dn_is_config(char *dn)
  774. {
  775. int ret = 0;
  776. Slapi_DN *sdn = NULL;
  777. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  778. "--> automember_dn_is_config\n");
  779. if (dn == NULL) {
  780. goto bail;
  781. }
  782. sdn = slapi_sdn_new_dn_byref(dn);
  783. /* If an alternate config area is configured, treat it's child
  784. * entries as config entries. If the alternate config area is
  785. * not configured, treat children of the top-level plug-in
  786. * config entry as our config entries. */
  787. if (automember_get_config_area()) {
  788. if (slapi_sdn_issuffix(sdn, automember_get_config_area()) &&
  789. slapi_sdn_compare(sdn, automember_get_config_area())) {
  790. ret = 1;
  791. }
  792. } else {
  793. if (slapi_sdn_issuffix(sdn, automember_get_plugin_sdn()) &&
  794. slapi_sdn_compare(sdn, automember_get_plugin_sdn())) {
  795. ret = 1;
  796. }
  797. }
  798. bail:
  799. slapi_sdn_free(&sdn);
  800. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  801. "<-- automember_dn_is_config\n");
  802. return ret;
  803. }
  804. /*
  805. * automember_oktodo()
  806. *
  807. * Check if we want to process this operation. We need to be
  808. * sure that the operation succeeded.
  809. */
  810. static int
  811. automember_oktodo(Slapi_PBlock *pb)
  812. {
  813. int ret = 1;
  814. int oprc = 0;
  815. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  816. "--> automember_oktodo\n" );
  817. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0) {
  818. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  819. "automember_oktodo: could not get parameters\n" );
  820. ret = -1;
  821. }
  822. /* This plugin should only execute if the operation succeeded. */
  823. if(oprc != 0) {
  824. ret = 0;
  825. }
  826. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  827. "<-- automember_oktodo\n" );
  828. return ret;
  829. }
  830. /*
  831. * automember_isrepl()
  832. *
  833. * Returns 1 if the operation associated with pb
  834. * is a replicated op. Returns 0 otherwise.
  835. */
  836. static int
  837. automember_isrepl(Slapi_PBlock *pb)
  838. {
  839. int is_repl = 0;
  840. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  841. "--> automember_isrepl\n" );
  842. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  843. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  844. "<-- automember_isrepl\n" );
  845. return is_repl;
  846. }
  847. /*
  848. * automember_parse_regex_rule()
  849. *
  850. * Parses a regex rule and returns a regex rule struct. The caller
  851. * will need to free this struct when it is finished with it. If
  852. * there is a problem parsing the regex rule, an error will be
  853. * logged and NULL will be returned.
  854. */
  855. static struct automemberRegexRule *
  856. automember_parse_regex_rule(char *rule_string)
  857. {
  858. struct automemberRegexRule *rule = NULL;
  859. Slapi_DN *target_group_dn = NULL;
  860. char *desc = NULL;
  861. char *attr = NULL;
  862. Slapi_Regex *regex = NULL;
  863. const char *recomp_result = NULL;
  864. char *dn_string = NULL;
  865. char *p = NULL;
  866. char *p2 = NULL;
  867. /* A rule is in the form "target:desc:attr=regex" */
  868. /* Find the target group DN. */
  869. if ((p = strchr(rule_string, ':')) == NULL) {
  870. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  871. "automember_parse_regex_rule: Unable to parse "
  872. "regex rule (missing first ':' delimeter).\n");
  873. goto bail;
  874. }
  875. /* Ensure the target group DN is not empty. */
  876. if (p == rule_string) {
  877. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  878. "automember_parse_regex_rule: Unable to parse "
  879. " regex rule (missing target group DN).\n");
  880. goto bail;
  881. }
  882. if ((dn_string = strndup(rule_string, p - rule_string)) == NULL) {
  883. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  884. "automember_parse_regex_rule: Error allocating "
  885. "memory.\n");
  886. goto bail;
  887. }
  888. /* Ensure that the DN is valid. */
  889. if (slapi_dn_syntax_check(NULL, dn_string, 1) != 0) {
  890. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  891. "automember_parse_regex_rule: Unable to parse "
  892. " regex rule (invalid target group DN).\n");
  893. slapi_cn_free_string(&dn_string);
  894. goto bail;
  895. }
  896. /* Create a Slapi_DN. */
  897. target_group_dn = slapi_sdn_new_dn_passin(dn_string);
  898. /* Find the description. */
  899. p++;
  900. if (*p == '\0') {
  901. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  902. "automember_parse_regex_rule: Unable to parse "
  903. "regex rule (missing description).\n");
  904. goto bail;
  905. }
  906. p2 = p;
  907. if ((p = strchr(p2, ':')) == NULL) {
  908. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  909. "automember_parse_regex_rule: Unable to parse "
  910. "regex rule (missing second ':' delimeter).\n");
  911. goto bail;
  912. }
  913. /* We allow an empty description. */
  914. if (p == p2) {
  915. desc = slapi_ch_strdup("");
  916. } else {
  917. if ((desc = strndup(p2, p - p2)) == NULL) {
  918. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  919. "automember_parse_regex_rule: Unable to allocate "
  920. "memory.\n");
  921. goto bail;
  922. }
  923. }
  924. /* Find the comparison attribute name. */
  925. p++;
  926. if (*p == '\0') {
  927. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  928. "automember_parse_regex_rule: Unable to parse "
  929. "regex rule (missing comparison attribute).\n");
  930. goto bail;
  931. }
  932. p2 = p;
  933. if ((p = strchr(p2, '=')) == NULL) {
  934. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  935. "automember_parse_regex_rule: Unable to parse "
  936. "regex rule (missing '=' delimeter).\n");
  937. goto bail;
  938. }
  939. if ((attr = strndup(p2, p - p2)) == NULL) {
  940. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  941. "automember_parse_regex_rule: Unable to allocate "
  942. "memory.\n");
  943. goto bail;
  944. }
  945. /* Validate the attribute. */
  946. for (p2 = attr; p2 && (*p2 != '\0'); p2++) {
  947. if (!IS_ATTRDESC_CHAR(*p2)) {
  948. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  949. "automember_parse_regex_rule: Invalid comparison "
  950. "attribute name \"%s\".\n", attr);
  951. goto bail;
  952. }
  953. }
  954. /* Find the regex. */
  955. p++;
  956. if (*p == '\0') {
  957. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  958. "automember_parse_regex_rule: Unable to parse "
  959. "regex rule (missing regex).\n");
  960. goto bail;
  961. }
  962. /* Compile the regex to validate it. */
  963. regex = slapi_re_comp(p, &recomp_result);
  964. if (!regex) {
  965. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  966. "automember_parse_regex_rule: Unable to parse "
  967. "regex rule (invalid regex). Error \"%s\".\n",
  968. recomp_result?recomp_result:"unknown");
  969. }
  970. /* Validation has passed, so create the regex rule struct and fill it in.
  971. * We hand off everything we have allocated. All of this will be free'd
  972. * when the rule struct itself is freed. */
  973. rule = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  974. rule->target_group_dn = target_group_dn;
  975. rule->desc = desc;
  976. rule->attr = attr;
  977. rule->regex_str = slapi_ch_strdup(p);
  978. rule->regex = regex;
  979. bail:
  980. /* Cleanup if we didn't successfully create a rule. */
  981. if (!rule) {
  982. slapi_sdn_free(&target_group_dn);
  983. slapi_ch_free_string(&desc);
  984. slapi_ch_free_string(&attr);
  985. slapi_re_free(regex);
  986. }
  987. return rule;
  988. }
  989. /*
  990. * automember_free_regex_rule()
  991. *
  992. * Frees a regex rule and all of it's contents from memory.
  993. */
  994. static void
  995. automember_free_regex_rule(struct automemberRegexRule *rule)
  996. {
  997. if (rule) {
  998. if (rule->target_group_dn) {
  999. slapi_sdn_free(&(rule->target_group_dn));
  1000. }
  1001. if (rule->desc) {
  1002. slapi_ch_free_string(&(rule->desc));
  1003. }
  1004. if (rule->attr) {
  1005. slapi_ch_free_string(&(rule->attr));
  1006. }
  1007. if (rule->regex_str) {
  1008. slapi_ch_free_string(&(rule->regex_str));
  1009. }
  1010. if (rule->regex) {
  1011. slapi_re_free(rule->regex);
  1012. }
  1013. }
  1014. slapi_ch_free((void **)&rule);
  1015. }
  1016. /*
  1017. * automember_parse_grouping_attr()
  1018. *
  1019. * Parses a grouping attribute and grouping value from
  1020. * the passed in config string. Memory will be allocated
  1021. * for grouping_attr and grouping_value, so it is up to
  1022. * the called to free them when they are no longer needed.
  1023. * Returns 0 upon success and 1 upon failure.
  1024. */
  1025. static int
  1026. automember_parse_grouping_attr(char *value, char **grouping_attr, char **grouping_value)
  1027. {
  1028. int ret = 0;
  1029. char *p = NULL;
  1030. /* Clear out any existing type or value. */
  1031. slapi_ch_free_string(grouping_attr);
  1032. slapi_ch_free_string(grouping_value);
  1033. /* split out the type from the value (use the first ':') */
  1034. if ((p = strchr(value, ':')) == NULL) {
  1035. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1036. "automember_parse_grouping_attr: Value for grouping attribute "
  1037. "is not in the correct format. (value: \"%s\").\n", value);
  1038. ret = 1;
  1039. goto bail;
  1040. }
  1041. /* Ensure the type is not empty. */
  1042. if (p == value) {
  1043. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1044. "automember_parse_grouping_attr: Value for grouping attribute "
  1045. "is not in the correct format. The grouping attribute is missing. "
  1046. "(value: \"%s\").\n", value);
  1047. ret = 1;
  1048. goto bail;
  1049. }
  1050. /* Duplicate the type to be returned. */
  1051. *grouping_attr = strndup(value, p - value);
  1052. /* Advance p to point to the beginning of the value. */
  1053. p++;
  1054. while (*p == ' ') {
  1055. p++;
  1056. }
  1057. /* Ensure the value is not empty. */
  1058. if (*p == '\0') {
  1059. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1060. "automember_parse_grouping_attr: Value for grouping attribute "
  1061. "is not in the correct format. The grouping value is "
  1062. "missing. (value: \"%s\").\n", value);
  1063. ret = 1;
  1064. goto bail;
  1065. }
  1066. /* Duplicate the value to be returned. */
  1067. *grouping_value = slapi_ch_strdup(p);
  1068. /* Ensure that memory was allocated successfully. */
  1069. if ((*grouping_attr == NULL) || (*grouping_value == NULL)) {
  1070. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1071. "automember_parse_grouping_attr: Error allocating memory.\n");
  1072. ret = 1;
  1073. goto bail;
  1074. }
  1075. /* Ensure that the grouping attr is a legal attr name. */
  1076. for (p = *grouping_attr; p && (*p != '\0'); p++) {
  1077. if (!IS_ATTRDESC_CHAR(*p)) {
  1078. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1079. "automember_parse_grouping_attr: Invalid value for "
  1080. "grouping attribute. The grouping attribute type is "
  1081. "illegal. (type: \"%s\").\n", *grouping_attr);
  1082. ret = 1;
  1083. goto bail;
  1084. }
  1085. }
  1086. /* Ensure that the grouping value type is a legal attr name. */
  1087. for (p = *grouping_value; p && (*p != '\0'); p++) {
  1088. if (!IS_ATTRDESC_CHAR(*p)) {
  1089. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1090. "automember_parse_grouping_attr: Invalid value for "
  1091. "grouping attribute. The grouping value type is "
  1092. "illegal. (type: \"%s\").\n", *grouping_value);
  1093. ret = 1;
  1094. goto bail;
  1095. }
  1096. }
  1097. bail:
  1098. if (ret != 0) {
  1099. slapi_ch_free_string(grouping_attr);
  1100. slapi_ch_free_string(grouping_value);
  1101. }
  1102. return ret;
  1103. }
  1104. /*
  1105. * automember_update_membership()
  1106. *
  1107. * Determines which target groups need to be updated according to
  1108. * the rules in config, then performs the updates.
  1109. */
  1110. static void
  1111. automember_update_membership(struct configEntry *config, Slapi_Entry *e)
  1112. {
  1113. PRCList *rule = NULL;
  1114. struct automemberRegexRule *curr_rule = NULL;
  1115. PRCList exclusions;
  1116. PRCList targets;
  1117. struct automemberDNListItem *dnitem = NULL;
  1118. Slapi_DN *last = NULL;
  1119. PRCList *curr_exclusion = NULL;
  1120. char **vals = NULL;
  1121. int i = 0;
  1122. if (!config || !e) {
  1123. return;
  1124. }
  1125. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1126. "automember_update_membership: Processing \"%s\" "
  1127. "definition entry for candidate entry \"%s\".\n",
  1128. config->dn, slapi_entry_get_dn(e));
  1129. /* Initialize our lists that keep track of targets. */
  1130. PR_INIT_CLIST(&exclusions);
  1131. PR_INIT_CLIST(&targets);
  1132. /* Go through exclusive rules and build an exclusion list. */
  1133. if (config->exclusive_rules) {
  1134. if (!PR_CLIST_IS_EMPTY((PRCList *)config->exclusive_rules)) {
  1135. rule = PR_LIST_HEAD((PRCList *)config->exclusive_rules);
  1136. while (rule != (PRCList *)config->exclusive_rules) {
  1137. curr_rule = (struct automemberRegexRule *)rule;
  1138. /* Regex rules are sorted by the target group DN. This means
  1139. * we can skip all rules for the last target group DN that we
  1140. * added to the exclusions list. */
  1141. if ((last == NULL) || slapi_sdn_compare(last, curr_rule->target_group_dn) != 0) {
  1142. /* Get comparison attr and loop through values. */
  1143. vals = slapi_entry_attr_get_charray(e, curr_rule->attr);
  1144. for (i = 0; vals && vals[i]; ++i) {
  1145. /* Evaluate the regex. */
  1146. if (slapi_re_exec(curr_rule->regex, vals[i], -1) == 1) {
  1147. /* Found a match. Add to end of the exclusion list
  1148. * and set last as a hint to ourselves. */
  1149. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1150. "automember_update_membership: Adding \"%s\" "
  1151. "to list of excluded groups for \"%s\" "
  1152. "(matched: \"%s=%s\").\n",
  1153. slapi_sdn_get_dn(curr_rule->target_group_dn),
  1154. slapi_entry_get_dn(e), curr_rule->attr,
  1155. curr_rule->regex_str);
  1156. dnitem = (struct automemberDNListItem *)slapi_ch_calloc(1,
  1157. sizeof(struct automemberDNListItem));
  1158. /* We are just referencing the dn from the regex rule. We
  1159. * will not free it when we clean up this list. This list
  1160. * is more short-lived than the regex rule list, so we can
  1161. * get away with this optimization. */
  1162. dnitem->dn = curr_rule->target_group_dn;
  1163. PR_APPEND_LINK(&(dnitem->list), &exclusions);
  1164. last = curr_rule->target_group_dn;
  1165. }
  1166. }
  1167. slapi_ch_array_free(vals);
  1168. vals = NULL;
  1169. }
  1170. rule = PR_NEXT_LINK(rule);
  1171. }
  1172. }
  1173. }
  1174. /* Go through inclusive rules and build the target list. */
  1175. if (config->inclusive_rules) {
  1176. if (!PR_CLIST_IS_EMPTY((PRCList *)config->inclusive_rules)) {
  1177. /* Clear out our last hint from processing exclusions. */
  1178. last = NULL;
  1179. /* Get the first excluded target for exclusion checking. */
  1180. if (!PR_CLIST_IS_EMPTY(&exclusions)) {
  1181. curr_exclusion = PR_LIST_HEAD(&exclusions);
  1182. }
  1183. rule = PR_LIST_HEAD((PRCList *)config->inclusive_rules);
  1184. while (rule != (PRCList *)config->inclusive_rules) {
  1185. curr_rule = (struct automemberRegexRule *)rule;
  1186. /* The excluded targets are stored in alphabetical order. Instead
  1187. * of going through the entire exclusion list for each inclusive
  1188. * rule, we can simply go through the exclusion list once and keep
  1189. * track of our position. If the curent exclusion comes after
  1190. * the target DN used in the current inclusive rule, it can't be
  1191. * excluded. If the current exclusion comes before the target
  1192. * in the current rule, we need to go through the exclusion list
  1193. * until we find a target that is the same or comes after the
  1194. * current rule. */
  1195. if (curr_exclusion) {
  1196. while ((curr_exclusion != &exclusions) && (slapi_sdn_compare(
  1197. ((struct automemberDNListItem *)curr_exclusion)->dn,
  1198. curr_rule->target_group_dn) < 0)) {
  1199. curr_exclusion = PR_NEXT_LINK(curr_exclusion);
  1200. }
  1201. }
  1202. /* Regex rules are sorted by the target group DN. This means
  1203. * we can skip all rules for the last target group DN that we
  1204. * added to the targets list. We also skip any rules for
  1205. * target groups that have been excluded by an exclusion rule. */
  1206. if (((curr_exclusion == NULL) || (curr_exclusion == &exclusions) ||
  1207. slapi_sdn_compare(((struct automemberDNListItem *)curr_exclusion)->dn,
  1208. curr_rule->target_group_dn) != 0) && ((last == NULL) ||
  1209. (slapi_sdn_compare(last, curr_rule->target_group_dn) != 0))) {
  1210. /* Get comparison attr and loop through values. */
  1211. vals = slapi_entry_attr_get_charray(e, curr_rule->attr);
  1212. for (i = 0; vals && vals[i]; ++i) {
  1213. /* Evaluate the regex. */
  1214. if (slapi_re_exec(curr_rule->regex, vals[i], -1) == 1) {
  1215. /* Found a match. Add to the end of the targets list
  1216. * and set last as a hint to ourselves. */
  1217. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1218. "automember_update_membership: Adding \"%s\" "
  1219. "to list of target groups for \"%s\" "
  1220. "(matched: \"%s=%s\").\n",
  1221. slapi_sdn_get_dn(curr_rule->target_group_dn),
  1222. slapi_entry_get_dn(e), curr_rule->attr,
  1223. curr_rule->regex_str);
  1224. dnitem = (struct automemberDNListItem *)slapi_ch_calloc(1,
  1225. sizeof(struct automemberDNListItem));
  1226. /* We are just referencing the dn from the regex rule. We
  1227. * will not free it when we clean up this list. This list
  1228. * is more short-lived than the regex rule list, so we can
  1229. * get away with this optimization. */
  1230. dnitem->dn = curr_rule->target_group_dn;
  1231. PR_APPEND_LINK(&(dnitem->list), &targets);
  1232. last = curr_rule->target_group_dn;
  1233. }
  1234. }
  1235. slapi_ch_array_free(vals);
  1236. vals = NULL;
  1237. }
  1238. rule = PR_NEXT_LINK(rule);
  1239. }
  1240. }
  1241. }
  1242. /* If no targets, update default groups if set. Otherwise, update
  1243. * targets. Use a helper to do the actual updates. We can just pass an
  1244. * array of target group DNs along with our entry DN, the grouping attr,
  1245. * and the grouping value. */
  1246. if (PR_CLIST_IS_EMPTY(&targets)) {
  1247. /* Add to each default group. */
  1248. for (i = 0; config->default_groups && config->default_groups[i]; i++) {
  1249. automember_add_member_value(e, config->default_groups[i],
  1250. config->grouping_attr, config->grouping_value);
  1251. }
  1252. } else {
  1253. /* Update the target groups. */
  1254. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&targets);
  1255. while ((PRCList *)dnitem != &targets) {
  1256. automember_add_member_value(e, slapi_sdn_get_dn(dnitem->dn),
  1257. config->grouping_attr, config->grouping_value);
  1258. dnitem = (struct automemberDNListItem *)PR_NEXT_LINK((PRCList *)dnitem);
  1259. }
  1260. }
  1261. /* Free the exclusions and targets lists. Remember that the DN's
  1262. * are not ours, so don't free them! */
  1263. while (!PR_CLIST_IS_EMPTY(&exclusions)) {
  1264. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&exclusions);
  1265. PR_REMOVE_LINK((PRCList *)dnitem);
  1266. slapi_ch_free((void**)&dnitem);
  1267. }
  1268. while (!PR_CLIST_IS_EMPTY(&targets)) {
  1269. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&targets);
  1270. PR_REMOVE_LINK((PRCList *)dnitem);
  1271. slapi_ch_free((void**)&dnitem);
  1272. }
  1273. }
  1274. /*
  1275. * automember_add_member_value()
  1276. *
  1277. * Adds a member entry to a group.
  1278. */
  1279. static void
  1280. automember_add_member_value(Slapi_Entry *member_e, const char *group_dn,
  1281. char *grouping_attr, char *grouping_value)
  1282. {
  1283. Slapi_PBlock *mod_pb = slapi_pblock_new();
  1284. int result = LDAP_SUCCESS;
  1285. LDAPMod mod;
  1286. LDAPMod *mods[2];
  1287. char *vals[2];
  1288. char *member_value = NULL;
  1289. int freeit = 0;
  1290. /* If grouping_value is dn, we need to fetch the dn instead. */
  1291. if (slapi_attr_type_cmp(grouping_value, "dn", SLAPI_TYPE_CMP_EXACT) == 0) {
  1292. member_value = slapi_entry_get_ndn(member_e);
  1293. } else {
  1294. member_value = slapi_entry_attr_get_charptr(member_e, grouping_value);
  1295. freeit = 1;
  1296. }
  1297. if (member_value) {
  1298. /* Set up the operation. */
  1299. vals[0] = member_value;
  1300. vals[1] = 0;
  1301. mod.mod_op = LDAP_MOD_ADD;
  1302. mod.mod_type = grouping_attr;
  1303. mod.mod_values = vals;
  1304. mods[0] = &mod;
  1305. mods[1] = 0;
  1306. /* Perform the modify operation. */
  1307. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1308. "automember_add_member_value: Adding \"%s\" as "
  1309. "a \"%s\" value to group \"%s\".\n",
  1310. member_value, grouping_attr, group_dn);
  1311. slapi_modify_internal_set_pb(mod_pb, group_dn,
  1312. mods, 0, 0, automember_get_plugin_id(), 0);
  1313. slapi_modify_internal_pb(mod_pb);
  1314. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1315. if ((result != LDAP_SUCCESS) && (result != LDAP_TYPE_OR_VALUE_EXISTS)) {
  1316. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1317. "automember_add_member_value: Unable to add \"%s\" as "
  1318. "a \"%s\" value to group \"%s\" (%s).\n",
  1319. member_value, grouping_attr, group_dn,
  1320. ldap_err2string(result));
  1321. }
  1322. } else {
  1323. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1324. "automember_add_member_value: Unable to find grouping "
  1325. "value attribute \"%s\" in entry \"%s\".\n",
  1326. grouping_value, slapi_entry_get_dn(member_e));
  1327. }
  1328. /* Cleanup */
  1329. if (freeit) {
  1330. slapi_ch_free_string(&member_value);
  1331. }
  1332. slapi_pblock_destroy(mod_pb);
  1333. }
  1334. /*
  1335. * Operation callback functions
  1336. */
  1337. /*
  1338. * automember_pre_op()
  1339. *
  1340. * Checks if an operation affects the auto membership
  1341. * config and validates the config changes.
  1342. */
  1343. static int
  1344. automember_pre_op(Slapi_PBlock * pb, int modop)
  1345. {
  1346. char *dn = 0;
  1347. Slapi_Entry *e = 0;
  1348. Slapi_Mods *smods = 0;
  1349. LDAPMod **mods;
  1350. int free_entry = 0;
  1351. char *errstr = NULL;
  1352. int ret = 0;
  1353. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1354. "--> automember_pre_op\n");
  1355. /* Just bail if we aren't ready to service requests yet. */
  1356. if (!g_plugin_started)
  1357. goto bail;
  1358. if (0 == (dn = automember_get_dn(pb)))
  1359. goto bail;
  1360. if (automember_dn_is_config(dn)) {
  1361. /* Validate config changes, but don't apply them.
  1362. * This allows us to reject invalid config changes
  1363. * here at the pre-op stage. Applying the config
  1364. * needs to be done at the post-op stage. */
  1365. if (LDAP_CHANGETYPE_ADD == modop) {
  1366. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  1367. } else if (LDAP_CHANGETYPE_MODIFY == modop) {
  1368. /* Fetch the entry being modified so we can
  1369. * create the resulting entry for validation. */
  1370. Slapi_DN *tmp_dn = slapi_sdn_new_dn_byref(dn);
  1371. if (tmp_dn) {
  1372. slapi_search_internal_get_entry(tmp_dn, 0, &e, automember_get_plugin_id());
  1373. slapi_sdn_free(&tmp_dn);
  1374. free_entry = 1;
  1375. }
  1376. /* If the entry doesn't exist, just bail and
  1377. * let the server handle it. */
  1378. if (e == NULL) {
  1379. goto bail;
  1380. }
  1381. /* Grab the mods. */
  1382. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1383. smods = slapi_mods_new();
  1384. slapi_mods_init_byref(smods, mods);
  1385. /* Apply the mods to create the resulting entry. */
  1386. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1387. /* The mods don't apply cleanly, so we just let this op go
  1388. * to let the main server handle it. */
  1389. goto bailmod;
  1390. }
  1391. }
  1392. if (automember_parse_config_entry(e, 0) != 0) {
  1393. /* Refuse the operation if config parsing failed. */
  1394. ret = LDAP_UNWILLING_TO_PERFORM;
  1395. if (LDAP_CHANGETYPE_ADD == modop) {
  1396. errstr = slapi_ch_smprintf("Not a valid auto membership configuration entry.");
  1397. } else {
  1398. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1399. "auto membership configuration.");
  1400. }
  1401. }
  1402. }
  1403. bailmod:
  1404. /* Clean up smods. */
  1405. if (LDAP_CHANGETYPE_MODIFY == modop) {
  1406. slapi_mods_free(&smods);
  1407. }
  1408. bail:
  1409. if (free_entry && e)
  1410. slapi_entry_free(e);
  1411. if (ret) {
  1412. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1413. "automember_pre_op: operation failure [%d]\n", ret);
  1414. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  1415. slapi_ch_free((void **)&errstr);
  1416. ret = -1;
  1417. }
  1418. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1419. "<-- automember_pre_op\n");
  1420. return ret;
  1421. }
  1422. static int
  1423. automember_add_pre_op(Slapi_PBlock * pb)
  1424. {
  1425. return automember_pre_op(pb, LDAP_CHANGETYPE_ADD);
  1426. }
  1427. static int
  1428. automember_mod_pre_op(Slapi_PBlock * pb)
  1429. {
  1430. return automember_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  1431. }
  1432. /*
  1433. * automember_mod_post_op()
  1434. *
  1435. * Reloads the auto membership config
  1436. * if config changes were made.
  1437. */
  1438. static int
  1439. automember_mod_post_op(Slapi_PBlock *pb)
  1440. {
  1441. Slapi_Mods *smods = NULL;
  1442. char *dn = NULL;
  1443. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1444. "--> automember_mod_post_op\n");
  1445. /* Just bail if we aren't ready to service requests yet. */
  1446. if (!g_plugin_started)
  1447. return 0;
  1448. if (automember_oktodo(pb) && (dn = automember_get_dn(pb))) {
  1449. /* Check if the config is being modified and reload if so. */
  1450. if (automember_dn_is_config(dn)) {
  1451. automember_load_config();
  1452. }
  1453. }
  1454. bail:
  1455. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1456. "<-- automember_mod_post_op\n");
  1457. return 0;
  1458. }
  1459. static int
  1460. automember_add_post_op(Slapi_PBlock *pb)
  1461. {
  1462. Slapi_Entry *e = NULL;
  1463. char *dn = NULL;
  1464. struct configEntry *config = NULL;
  1465. PRCList *list = NULL;
  1466. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1467. "--> automember_add_post_op\n");
  1468. /* Just bail if we aren't ready to service requests yet. */
  1469. if (!g_plugin_started || !automember_oktodo(pb))
  1470. return 0;
  1471. /* Reload config if a config entry was added. */
  1472. if ((dn = automember_get_dn(pb))) {
  1473. if (automember_dn_is_config(dn)) {
  1474. automember_load_config();
  1475. }
  1476. } else {
  1477. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1478. "automember_add_post_op: Error "
  1479. "retrieving dn\n");
  1480. }
  1481. /* If replication, just bail. */
  1482. if (automember_isrepl(pb)) {
  1483. return 0;
  1484. }
  1485. /* Get the newly added entry. */
  1486. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1487. if (e) {
  1488. /* If the entry is a tombstone, just bail. */
  1489. if (slapi_entry_attr_hasvalue(e, SLAPI_ATTR_OBJECTCLASS,
  1490. SLAPI_ATTR_VALUE_TOMBSTONE)) {
  1491. return 0;
  1492. }
  1493. /* Check if a config entry applies
  1494. * to the entry being added. */
  1495. automember_config_read_lock();
  1496. /* Bail out if the plug-in close function was just called. */
  1497. if (!g_plugin_started) {
  1498. automember_config_unlock();
  1499. return 0;
  1500. }
  1501. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  1502. list = PR_LIST_HEAD(g_automember_config);
  1503. while (list != g_automember_config) {
  1504. config = (struct configEntry *)list;
  1505. /* Does the entry meet scope and filter requirements? */
  1506. if (slapi_dn_issuffix(dn, config->scope) &&
  1507. (slapi_filter_test_simple(e, config->filter) == 0)) {
  1508. /* Find out what membership changes are needed and make them. */
  1509. automember_update_membership(config, e);
  1510. }
  1511. list = PR_NEXT_LINK(list);
  1512. }
  1513. }
  1514. automember_config_unlock();
  1515. } else {
  1516. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1517. "automember_add_post_op: Error "
  1518. "retrieving post-op entry %s\n", dn);
  1519. }
  1520. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1521. "<-- automember_add_post_op\n");
  1522. return 0;
  1523. }
  1524. /*
  1525. * automember_del_post_op()
  1526. *
  1527. * Removes deleted config.
  1528. */
  1529. static int
  1530. automember_del_post_op(Slapi_PBlock *pb)
  1531. {
  1532. char *dn = NULL;
  1533. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1534. "--> automember_del_post_op\n");
  1535. /* Just bail if we aren't ready to service requests yet. */
  1536. if (!g_plugin_started || !automember_oktodo(pb)) {
  1537. return 0;
  1538. }
  1539. /* Reload config if a config entry was deleted. */
  1540. if ((dn = automember_get_dn(pb))) {
  1541. if (automember_dn_is_config(dn))
  1542. automember_load_config();
  1543. } else {
  1544. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1545. "automember_del_post_op: Error "
  1546. "retrieving dn\n");
  1547. }
  1548. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1549. "<-- automember_del_post_op\n");
  1550. return 0;
  1551. }
  1552. /*
  1553. * automember_modrdn_post_op()
  1554. *
  1555. * Reloads config if config entries move
  1556. * into or out of our config scope.
  1557. */
  1558. static int
  1559. automember_modrdn_post_op(Slapi_PBlock *pb)
  1560. {
  1561. char *old_dn = NULL;
  1562. char *new_dn = NULL;
  1563. Slapi_Entry *post_e = NULL;
  1564. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1565. "--> automember_modrdn_post_op\n");
  1566. /* Just bail if we aren't ready to service requests yet. */
  1567. if (!g_plugin_started || !automember_oktodo(pb))
  1568. return 0;
  1569. /* Reload config if an existing config entry was renamed,
  1570. * or if the new dn brings an entry into the scope of the
  1571. * config entries. */
  1572. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  1573. if (post_e) {
  1574. new_dn = slapi_entry_get_ndn(post_e);
  1575. } else {
  1576. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1577. "automember_modrdn_post_op: Error "
  1578. "retrieving post-op entry\n");
  1579. return 0;
  1580. }
  1581. if ((old_dn = automember_get_dn(pb))) {
  1582. if (automember_dn_is_config(old_dn) || automember_dn_is_config(new_dn))
  1583. automember_load_config();
  1584. } else {
  1585. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1586. "automember_modrdn_post_op: Error "
  1587. "retrieving dn\n");
  1588. }
  1589. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1590. "<-- automember_modrdn_post_op\n");
  1591. return 0;
  1592. }