automember.c 63 KB

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