automember.c 68 KB

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