automember.c 64 KB

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