automember.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2011 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * Auto Membership Plug-in
  42. */
  43. #include "automember.h"
  44. /*
  45. * Plug-in globals
  46. */
  47. static PRCList *g_automember_config = NULL;
  48. static Slapi_RWLock *g_automember_config_lock;
  49. static void *_PluginID = NULL;
  50. static Slapi_DN *_PluginDN = NULL;
  51. static Slapi_DN *_ConfigAreaDN = NULL;
  52. static int g_plugin_started = 0;
  53. static Slapi_PluginDesc pdesc = { AUTOMEMBER_FEATURE_DESC,
  54. VENDOR,
  55. DS_PACKAGE_VERSION,
  56. AUTOMEMBER_PLUGIN_DESC };
  57. /*
  58. * Plug-in management functions
  59. */
  60. int automember_init(Slapi_PBlock * pb);
  61. static int automember_start(Slapi_PBlock * pb);
  62. static int automember_close(Slapi_PBlock * pb);
  63. static int automember_postop_init(Slapi_PBlock * pb);
  64. static int automember_internal_postop_init(Slapi_PBlock *pb);
  65. /*
  66. * Operation callbacks (where the real work is done)
  67. */
  68. static int automember_mod_post_op(Slapi_PBlock *pb);
  69. static int automember_add_post_op(Slapi_PBlock *pb);
  70. static int automember_del_post_op(Slapi_PBlock *pb);
  71. static int automember_modrdn_post_op(Slapi_PBlock *pb);
  72. static int automember_pre_op(Slapi_PBlock *pb, int modop);
  73. static int automember_mod_pre_op(Slapi_PBlock *pb);
  74. static int automember_add_pre_op(Slapi_PBlock *pb);
  75. /*
  76. * Config cache management functions
  77. */
  78. static int automember_load_config();
  79. static void automember_delete_config();
  80. static int automember_parse_config_entry(Slapi_Entry * e, int apply);
  81. static void automember_free_config_entry(struct configEntry ** entry);
  82. /*
  83. * helpers
  84. */
  85. static Slapi_DN *automember_get_sdn(Slapi_PBlock *pb);
  86. static Slapi_DN *automember_get_config_area();
  87. static void automember_set_config_area(Slapi_DN *sdn);
  88. static int automember_dn_is_config(Slapi_DN *sdn);
  89. static int automember_oktodo(Slapi_PBlock *pb);
  90. static int automember_isrepl(Slapi_PBlock *pb);
  91. static void automember_parse_regex_entry(struct configEntry *config, Slapi_Entry *e);
  92. static struct automemberRegexRule *automember_parse_regex_rule(char *rule_string);
  93. static void automember_free_regex_rule(struct automemberRegexRule *rule);
  94. static int automember_parse_grouping_attr(char *value, char **grouping_attr,
  95. char **grouping_value);
  96. static void automember_update_membership(struct configEntry *config, Slapi_Entry *e);
  97. static void automember_add_member_value(Slapi_Entry *member_e, const char *group_dn,
  98. char *grouping_attr, char *grouping_value);
  99. /*
  100. * Config cache locking functions
  101. */
  102. void
  103. automember_config_read_lock()
  104. {
  105. slapi_rwlock_rdlock(g_automember_config_lock);
  106. }
  107. void
  108. automember_config_write_lock()
  109. {
  110. slapi_rwlock_wrlock(g_automember_config_lock);
  111. }
  112. void
  113. automember_config_unlock()
  114. {
  115. slapi_rwlock_unlock(g_automember_config_lock);
  116. }
  117. /*
  118. * Plugin identity functions
  119. */
  120. void
  121. automember_set_plugin_id(void *pluginID)
  122. {
  123. _PluginID = pluginID;
  124. }
  125. void *
  126. automember_get_plugin_id()
  127. {
  128. return _PluginID;
  129. }
  130. void
  131. automember_set_plugin_sdn(Slapi_DN *pluginDN)
  132. {
  133. _PluginDN = pluginDN;
  134. }
  135. Slapi_DN *
  136. automember_get_plugin_sdn()
  137. {
  138. return _PluginDN;
  139. }
  140. /*
  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. Slapi_DN *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 = slapi_new_rwlock();
  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_SDN, &plugindn);
  264. if (NULL == plugindn || 0 == slapi_sdn_get_ndn_len(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_dup(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 this is a new config entry being added, it won't exist yet
  546. * when we are simply validating config. We can just ignore no
  547. * such object errors. */
  548. if ((LDAP_SUCCESS != result) && (LDAP_NO_SUCH_OBJECT != result)) {
  549. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  550. "automember_parse_config_entry: Error searching "
  551. "for child rule entries for config \"%s\" (err=%d).",
  552. entry->dn, result);
  553. ret = -1;
  554. goto bail;
  555. }
  556. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  557. &rule_entries);
  558. /* Go through each child rule entry and parse it. */
  559. for (i = 0; rule_entries && (rule_entries[i] != NULL); i++) {
  560. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  561. "automember_parse_config_entry: parsing regex rule entry "
  562. "\"%s\".\n", slapi_entry_get_dn(rule_entries[i]));
  563. automember_parse_regex_entry(entry, rule_entries[i]);
  564. }
  565. /* If we were only called to validate config, we can
  566. * just bail out before applying the config changes */
  567. if (apply == 0) {
  568. goto bail;
  569. }
  570. /* Add the config object to the list. We order by scope. */
  571. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  572. list = PR_LIST_HEAD(g_automember_config);
  573. while (list != g_automember_config) {
  574. config_entry = (struct configEntry *) list;
  575. /* If the config entry we are adding has a scope that is
  576. * a child of the scope of the current list item, we insert
  577. * the entry before that list item. */
  578. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  579. PR_INSERT_BEFORE(&(entry->list), list);
  580. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  581. "store [%s] before [%s] \n", entry->dn,
  582. config_entry->dn);
  583. entry_added = 1;
  584. break;
  585. }
  586. list = PR_NEXT_LINK(list);
  587. /* If we hit the end of the list, add to the tail. */
  588. if (g_automember_config == list) {
  589. PR_INSERT_BEFORE(&(entry->list), list);
  590. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  591. "store [%s] at tail\n", entry->dn);
  592. entry_added = 1;
  593. break;
  594. }
  595. }
  596. } else {
  597. /* first entry */
  598. PR_INSERT_LINK(&(entry->list), g_automember_config);
  599. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  600. "store [%s] at head \n", entry->dn);
  601. entry_added = 1;
  602. }
  603. bail:
  604. if (0 == entry_added) {
  605. /* Don't log error if we weren't asked to apply config */
  606. if ((apply != 0) && (entry != NULL)) {
  607. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  608. "automember_parse_config_entry: Invalid config entry "
  609. "[%s] skipped\n", entry->dn);
  610. }
  611. automember_free_config_entry(&entry);
  612. } else {
  613. ret = 0;
  614. }
  615. slapi_ch_free_string(&filter_str);
  616. slapi_filter_free(filter, 1);
  617. slapi_free_search_results_internal(search_pb);
  618. slapi_pblock_destroy(search_pb);
  619. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  620. "<-- automember_parse_config_entry\n");
  621. return ret;
  622. }
  623. static void
  624. automember_free_config_entry(struct configEntry ** entry)
  625. {
  626. struct configEntry *e = *entry;
  627. if (e == NULL)
  628. return;
  629. if (e->dn) {
  630. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  631. "freeing config entry [%s]\n", e->dn);
  632. slapi_ch_free_string(&e->dn);
  633. }
  634. if (e->scope) {
  635. slapi_ch_free_string(&e->scope);
  636. }
  637. if (e->filter) {
  638. slapi_filter_free(e->filter, 1);
  639. }
  640. if (e->exclusive_rules) {
  641. PRCList *list;
  642. /* Clear out the list contents. */
  643. while (!PR_CLIST_IS_EMPTY((PRCList *)e->exclusive_rules)) {
  644. list = PR_LIST_HEAD((PRCList *)e->exclusive_rules);
  645. PR_REMOVE_LINK(list);
  646. automember_free_regex_rule((struct automemberRegexRule *)list);
  647. }
  648. /* Free the list itself. */
  649. slapi_ch_free((void **)&(e->exclusive_rules));
  650. }
  651. /* Clear out the list contents. */
  652. if (e->inclusive_rules) {
  653. PRCList *list;
  654. while (!PR_CLIST_IS_EMPTY((PRCList *)e->inclusive_rules)) {
  655. list = PR_LIST_HEAD((PRCList *)e->inclusive_rules);
  656. PR_REMOVE_LINK(list);
  657. automember_free_regex_rule((struct automemberRegexRule *)list);
  658. }
  659. /* Free the list itself. */
  660. slapi_ch_free((void **)&(e->inclusive_rules));
  661. }
  662. if (e->default_groups) {
  663. slapi_ch_array_free(e->default_groups);
  664. }
  665. if (e->grouping_attr) {
  666. slapi_ch_free_string(&e->grouping_attr);
  667. }
  668. if (e->grouping_value) {
  669. slapi_ch_free_string(&e->grouping_value);
  670. }
  671. slapi_ch_free((void **)entry);
  672. }
  673. static void
  674. automember_delete_configEntry(PRCList *entry)
  675. {
  676. PR_REMOVE_LINK(entry);
  677. automember_free_config_entry((struct configEntry **) &entry);
  678. }
  679. static void
  680. automember_delete_config()
  681. {
  682. PRCList *list;
  683. /* Delete the config cache. */
  684. while (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  685. list = PR_LIST_HEAD(g_automember_config);
  686. automember_delete_configEntry(list);
  687. }
  688. return;
  689. }
  690. static Slapi_DN *
  691. automember_get_sdn(Slapi_PBlock * pb)
  692. {
  693. Slapi_DN *sdn = 0;
  694. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  695. "--> automember_get_sdn\n");
  696. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  697. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  698. "<-- automember_get_sdn\n");
  699. return sdn;
  700. }
  701. void
  702. automember_set_config_area(Slapi_DN *sdn)
  703. {
  704. _ConfigAreaDN = sdn;
  705. }
  706. Slapi_DN *
  707. automember_get_config_area()
  708. {
  709. return _ConfigAreaDN;
  710. }
  711. /*
  712. * automember_dn_is_config()
  713. *
  714. * Checks if dn is an auto membership config entry.
  715. */
  716. static int
  717. automember_dn_is_config(Slapi_DN *sdn)
  718. {
  719. int ret = 0;
  720. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  721. "--> automember_dn_is_config\n");
  722. if (sdn == NULL) {
  723. goto bail;
  724. }
  725. /* If an alternate config area is configured, treat it's child
  726. * entries as config entries. If the alternate config area is
  727. * not configured, treat children of the top-level plug-in
  728. * config entry as our config entries. */
  729. if (automember_get_config_area()) {
  730. if (slapi_sdn_issuffix(sdn, automember_get_config_area()) &&
  731. slapi_sdn_compare(sdn, automember_get_config_area())) {
  732. ret = 1;
  733. }
  734. } else {
  735. if (slapi_sdn_issuffix(sdn, automember_get_plugin_sdn()) &&
  736. slapi_sdn_compare(sdn, automember_get_plugin_sdn())) {
  737. ret = 1;
  738. }
  739. }
  740. bail:
  741. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  742. "<-- automember_dn_is_config\n");
  743. return ret;
  744. }
  745. /*
  746. * automember_oktodo()
  747. *
  748. * Check if we want to process this operation. We need to be
  749. * sure that the operation succeeded.
  750. */
  751. static int
  752. automember_oktodo(Slapi_PBlock *pb)
  753. {
  754. int ret = 1;
  755. int oprc = 0;
  756. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  757. "--> automember_oktodo\n" );
  758. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0) {
  759. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  760. "automember_oktodo: could not get parameters\n" );
  761. ret = -1;
  762. }
  763. /* This plugin should only execute if the operation succeeded. */
  764. if(oprc != 0) {
  765. ret = 0;
  766. }
  767. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  768. "<-- automember_oktodo\n" );
  769. return ret;
  770. }
  771. /*
  772. * automember_isrepl()
  773. *
  774. * Returns 1 if the operation associated with pb
  775. * is a replicated op. Returns 0 otherwise.
  776. */
  777. static int
  778. automember_isrepl(Slapi_PBlock *pb)
  779. {
  780. int is_repl = 0;
  781. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  782. "--> automember_isrepl\n" );
  783. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  784. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  785. "<-- automember_isrepl\n" );
  786. return is_repl;
  787. }
  788. /*
  789. * automember_parse_regex_entry()
  790. *
  791. * Parses a rule entry and adds the regex rules to the
  792. * passed in config struct. Invalid regex rules will
  793. * be skipped and logged at the fatal log level.
  794. */
  795. static void
  796. automember_parse_regex_entry(struct configEntry *config, Slapi_Entry *e)
  797. {
  798. char *target_group = NULL;
  799. char **values = NULL;
  800. PRCList *list;
  801. int i = 0;
  802. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  803. "--> automember_parse_regex_entry\n");
  804. /* Make sure the target group was specified. */
  805. target_group = slapi_entry_attr_get_charptr(e, AUTOMEMBER_TARGET_GROUP_TYPE);
  806. if (!target_group) {
  807. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  808. "automember_parse_regex_entry: The %s config "
  809. "setting is required for rule entry \"%s\".\n",
  810. AUTOMEMBER_TARGET_GROUP_TYPE, slapi_entry_get_ndn(e));
  811. goto bail;
  812. }
  813. /* Ensure that the target group DN is valid. */
  814. if (slapi_dn_syntax_check(NULL, target_group, 1) != 0) {
  815. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  816. "automember_parse_regex_entry: invalid target group DN "
  817. "in rule \"%s\" (dn=\"%s\").\n", slapi_entry_get_ndn(e),
  818. target_group);
  819. goto bail;
  820. }
  821. /* Load inclusive rules */
  822. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_INC_REGEX_TYPE);
  823. if (values) {
  824. struct automemberRegexRule *rule = NULL;
  825. /* If we haven't loaded any inclusive rules for
  826. * this config definition yet, create a new list. */
  827. if (config->inclusive_rules == NULL) {
  828. /* Create a list to hold our regex rules */
  829. config->inclusive_rules = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  830. PR_INIT_CLIST((PRCList *)config->inclusive_rules);
  831. }
  832. for (i = 0; values && values[i]; ++i) {
  833. rule = automember_parse_regex_rule(values[i]);
  834. if (rule) {
  835. /* Fill in the target group. */
  836. rule->target_group_dn = slapi_sdn_new_dn_byval(target_group);
  837. if (!PR_CLIST_IS_EMPTY((PRCList *)config->inclusive_rules)) {
  838. list = PR_LIST_HEAD((PRCList *)config->inclusive_rules);
  839. while (list != (PRCList *)config->inclusive_rules) {
  840. struct automemberRegexRule *curr_rule = (struct automemberRegexRule *)list;
  841. /* Order rules by target group DN */
  842. if (slapi_sdn_compare(rule->target_group_dn, curr_rule->target_group_dn) < 0) {
  843. PR_INSERT_BEFORE(&(rule->list), list);
  844. break;
  845. }
  846. list = PR_NEXT_LINK(list);
  847. /* If we hit the end of the list, add to the tail. */
  848. if ((PRCList *)config->inclusive_rules == list) {
  849. PR_INSERT_BEFORE(&(rule->list), list);
  850. break;
  851. }
  852. }
  853. } else {
  854. /* Add to head of list */
  855. PR_INSERT_LINK(&(rule->list), (PRCList *)config->inclusive_rules);
  856. }
  857. } else {
  858. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  859. "automember_parse_regex_entry: Skipping invalid inclusive "
  860. "regex rule in rule entry \"%s\" (rule = \"%s\").\n",
  861. slapi_entry_get_ndn(e), values[i]);
  862. }
  863. }
  864. slapi_ch_array_free(values);
  865. values = NULL;
  866. }
  867. /* Load exclusive rules. */
  868. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_EXC_REGEX_TYPE);
  869. if (values) {
  870. struct automemberRegexRule *rule = NULL;
  871. /* If we haven't loaded any exclusive rules for
  872. * this config definition yet, create a new list. */
  873. if (config->exclusive_rules == NULL) {
  874. /* Create a list to hold our regex rules */
  875. config->exclusive_rules = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  876. PR_INIT_CLIST((PRCList *)config->exclusive_rules);
  877. }
  878. for (i = 0; values && values[i]; ++i) {
  879. rule = automember_parse_regex_rule(values[i]);
  880. if (rule) {
  881. /* Fill in the target group. */
  882. rule->target_group_dn = slapi_sdn_new_dn_byval(target_group);
  883. if (!PR_CLIST_IS_EMPTY((PRCList *)config->exclusive_rules)) {
  884. list = PR_LIST_HEAD((PRCList *)config->exclusive_rules);
  885. while (list != (PRCList *)config->exclusive_rules) {
  886. struct automemberRegexRule *curr_rule = (struct automemberRegexRule *)list;
  887. /* Order rules by target group DN */
  888. if (slapi_sdn_compare(rule->target_group_dn, curr_rule->target_group_dn) < 0) {
  889. PR_INSERT_BEFORE(&(rule->list), list);
  890. break;
  891. }
  892. list = PR_NEXT_LINK(list);
  893. /* If we hit the end of the list, add to the tail. */
  894. if ((PRCList *)config->exclusive_rules == list) {
  895. PR_INSERT_BEFORE(&(rule->list), list);
  896. break;
  897. }
  898. }
  899. } else {
  900. /* Add to head of list */
  901. PR_INSERT_LINK(&(rule->list), (PRCList *)config->exclusive_rules);
  902. }
  903. } else {
  904. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  905. "automember_parse_regex_entry: Skipping invalid exclusive "
  906. "regex rule in rule entry \"%s\" (rule = \"%s\").\n",
  907. slapi_entry_get_ndn(e), values[i]);
  908. }
  909. }
  910. slapi_ch_array_free(values);
  911. values = NULL;
  912. }
  913. bail:
  914. slapi_ch_free_string(&target_group);
  915. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  916. "<-- automember_parse_regex_entry\n");
  917. }
  918. /*
  919. * automember_parse_regex_rule()
  920. *
  921. * Parses a regex rule and returns a regex rule struct. The caller
  922. * will need to free this struct when it is finished with it. If
  923. * there is a problem parsing the regex rule, an error will be
  924. * logged and NULL will be returned.
  925. */
  926. static struct automemberRegexRule *
  927. automember_parse_regex_rule(char *rule_string)
  928. {
  929. struct automemberRegexRule *rule = NULL;
  930. char *attr = NULL;
  931. Slapi_Regex *regex = NULL;
  932. const char *recomp_result = NULL;
  933. char *p = NULL;
  934. char *p2 = NULL;
  935. /* A rule is in the form "attr=regex". */
  936. /* Find the comparison attribute name. */
  937. if ((p = strchr(rule_string, '=')) == NULL) {
  938. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  939. "automember_parse_regex_rule: Unable to parse "
  940. "regex rule (missing '=' delimeter).\n");
  941. goto bail;
  942. }
  943. /* Make sure the attribute name is not empty. */
  944. if (p == rule_string) {
  945. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  946. "automember_parse_regex_rule: Unable to parse "
  947. " regex rule (missing comparison attribute).\n");
  948. goto bail;
  949. }
  950. if ((attr = strndup(rule_string, p - rule_string)) == NULL) {
  951. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  952. "automember_parse_regex_rule: Unable to allocate "
  953. "memory.\n");
  954. goto bail;
  955. }
  956. /* Validate the attribute. */
  957. for (p2 = attr; p2 && (*p2 != '\0'); p2++) {
  958. if (!IS_ATTRDESC_CHAR(*p2)) {
  959. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  960. "automember_parse_regex_rule: Invalid comparison "
  961. "attribute name \"%s\".\n", attr);
  962. goto bail;
  963. }
  964. }
  965. /* Find the regex. */
  966. p++;
  967. if (*p == '\0') {
  968. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  969. "automember_parse_regex_rule: Unable to parse "
  970. "regex rule (missing regex).\n");
  971. goto bail;
  972. }
  973. /* Compile the regex to validate it. */
  974. regex = slapi_re_comp(p, &recomp_result);
  975. if (!regex) {
  976. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  977. "automember_parse_regex_rule: Unable to parse "
  978. "regex rule (invalid regex). Error \"%s\".\n",
  979. recomp_result?recomp_result:"unknown");
  980. }
  981. /* Validation has passed, so create the regex rule struct and fill it in.
  982. * We hand off everything we have allocated. All of this will be free'd
  983. * when the rule struct itself is freed. */
  984. rule = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  985. rule->attr = attr;
  986. rule->regex_str = slapi_ch_strdup(p);
  987. rule->regex = regex;
  988. bail:
  989. /* Cleanup if we didn't successfully create a rule. */
  990. if (!rule) {
  991. slapi_ch_free_string(&attr);
  992. slapi_re_free(regex);
  993. }
  994. return rule;
  995. }
  996. /*
  997. * automember_free_regex_rule()
  998. *
  999. * Frees a regex rule and all of it's contents from memory.
  1000. */
  1001. static void
  1002. automember_free_regex_rule(struct automemberRegexRule *rule)
  1003. {
  1004. if (rule) {
  1005. if (rule->target_group_dn) {
  1006. slapi_sdn_free(&(rule->target_group_dn));
  1007. }
  1008. if (rule->attr) {
  1009. slapi_ch_free_string(&(rule->attr));
  1010. }
  1011. if (rule->regex_str) {
  1012. slapi_ch_free_string(&(rule->regex_str));
  1013. }
  1014. if (rule->regex) {
  1015. slapi_re_free(rule->regex);
  1016. }
  1017. }
  1018. slapi_ch_free((void **)&rule);
  1019. }
  1020. /*
  1021. * automember_parse_grouping_attr()
  1022. *
  1023. * Parses a grouping attribute and grouping value from
  1024. * the passed in config string. Memory will be allocated
  1025. * for grouping_attr and grouping_value, so it is up to
  1026. * the called to free them when they are no longer needed.
  1027. * Returns 0 upon success and 1 upon failure.
  1028. */
  1029. static int
  1030. automember_parse_grouping_attr(char *value, char **grouping_attr, char **grouping_value)
  1031. {
  1032. int ret = 0;
  1033. char *p = NULL;
  1034. /* Clear out any existing type or value. */
  1035. slapi_ch_free_string(grouping_attr);
  1036. slapi_ch_free_string(grouping_value);
  1037. /* split out the type from the value (use the first ':') */
  1038. if ((p = strchr(value, ':')) == NULL) {
  1039. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1040. "automember_parse_grouping_attr: Value for grouping attribute "
  1041. "is not in the correct format. (value: \"%s\").\n", value);
  1042. ret = 1;
  1043. goto bail;
  1044. }
  1045. /* Ensure the type is not empty. */
  1046. if (p == value) {
  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. The grouping attribute is missing. "
  1050. "(value: \"%s\").\n", value);
  1051. ret = 1;
  1052. goto bail;
  1053. }
  1054. /* Duplicate the type to be returned. */
  1055. *grouping_attr = strndup(value, p - value);
  1056. /* Advance p to point to the beginning of the value. */
  1057. p++;
  1058. while (*p == ' ') {
  1059. p++;
  1060. }
  1061. /* Ensure the value is not empty. */
  1062. if (*p == '\0') {
  1063. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1064. "automember_parse_grouping_attr: Value for grouping attribute "
  1065. "is not in the correct format. The grouping value is "
  1066. "missing. (value: \"%s\").\n", value);
  1067. ret = 1;
  1068. goto bail;
  1069. }
  1070. /* Duplicate the value to be returned. */
  1071. *grouping_value = slapi_ch_strdup(p);
  1072. /* Ensure that memory was allocated successfully. */
  1073. if ((*grouping_attr == NULL) || (*grouping_value == NULL)) {
  1074. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1075. "automember_parse_grouping_attr: Error allocating memory.\n");
  1076. ret = 1;
  1077. goto bail;
  1078. }
  1079. /* Ensure that the grouping attr is a legal attr name. */
  1080. for (p = *grouping_attr; p && (*p != '\0'); p++) {
  1081. if (!IS_ATTRDESC_CHAR(*p)) {
  1082. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1083. "automember_parse_grouping_attr: Invalid value for "
  1084. "grouping attribute. The grouping attribute type is "
  1085. "illegal. (type: \"%s\").\n", *grouping_attr);
  1086. ret = 1;
  1087. goto bail;
  1088. }
  1089. }
  1090. /* Ensure that the grouping value type is a legal attr name. */
  1091. for (p = *grouping_value; p && (*p != '\0'); p++) {
  1092. if (!IS_ATTRDESC_CHAR(*p)) {
  1093. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1094. "automember_parse_grouping_attr: Invalid value for "
  1095. "grouping attribute. The grouping value type is "
  1096. "illegal. (type: \"%s\").\n", *grouping_value);
  1097. ret = 1;
  1098. goto bail;
  1099. }
  1100. }
  1101. bail:
  1102. if (ret != 0) {
  1103. slapi_ch_free_string(grouping_attr);
  1104. slapi_ch_free_string(grouping_value);
  1105. }
  1106. return ret;
  1107. }
  1108. /*
  1109. * automember_update_membership()
  1110. *
  1111. * Determines which target groups need to be updated according to
  1112. * the rules in config, then performs the updates.
  1113. */
  1114. static void
  1115. automember_update_membership(struct configEntry *config, Slapi_Entry *e)
  1116. {
  1117. PRCList *rule = NULL;
  1118. struct automemberRegexRule *curr_rule = NULL;
  1119. PRCList exclusions;
  1120. PRCList targets;
  1121. struct automemberDNListItem *dnitem = NULL;
  1122. Slapi_DN *last = NULL;
  1123. PRCList *curr_exclusion = NULL;
  1124. char **vals = NULL;
  1125. int i = 0;
  1126. if (!config || !e) {
  1127. return;
  1128. }
  1129. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1130. "automember_update_membership: Processing \"%s\" "
  1131. "definition entry for candidate entry \"%s\".\n",
  1132. config->dn, slapi_entry_get_dn(e));
  1133. /* Initialize our lists that keep track of targets. */
  1134. PR_INIT_CLIST(&exclusions);
  1135. PR_INIT_CLIST(&targets);
  1136. /* Go through exclusive rules and build an exclusion list. */
  1137. if (config->exclusive_rules) {
  1138. if (!PR_CLIST_IS_EMPTY((PRCList *)config->exclusive_rules)) {
  1139. rule = PR_LIST_HEAD((PRCList *)config->exclusive_rules);
  1140. while (rule != (PRCList *)config->exclusive_rules) {
  1141. curr_rule = (struct automemberRegexRule *)rule;
  1142. /* Regex rules are sorted by the target group DN. This means
  1143. * we can skip all rules for the last target group DN that we
  1144. * added to the exclusions list. */
  1145. if ((last == NULL) || slapi_sdn_compare(last, curr_rule->target_group_dn) != 0) {
  1146. /* Get comparison attr and loop through values. */
  1147. vals = slapi_entry_attr_get_charray(e, curr_rule->attr);
  1148. for (i = 0; vals && vals[i]; ++i) {
  1149. /* Evaluate the regex. */
  1150. if (slapi_re_exec(curr_rule->regex, vals[i], -1) == 1) {
  1151. /* Found a match. Add to end of the exclusion list
  1152. * and set last as a hint to ourselves. */
  1153. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1154. "automember_update_membership: Adding \"%s\" "
  1155. "to list of excluded groups for \"%s\" "
  1156. "(matched: \"%s=%s\").\n",
  1157. slapi_sdn_get_dn(curr_rule->target_group_dn),
  1158. slapi_entry_get_dn(e), curr_rule->attr,
  1159. curr_rule->regex_str);
  1160. dnitem = (struct automemberDNListItem *)slapi_ch_calloc(1,
  1161. sizeof(struct automemberDNListItem));
  1162. /* We are just referencing the dn from the regex rule. We
  1163. * will not free it when we clean up this list. This list
  1164. * is more short-lived than the regex rule list, so we can
  1165. * get away with this optimization. */
  1166. dnitem->dn = curr_rule->target_group_dn;
  1167. PR_APPEND_LINK(&(dnitem->list), &exclusions);
  1168. last = curr_rule->target_group_dn;
  1169. }
  1170. }
  1171. slapi_ch_array_free(vals);
  1172. vals = NULL;
  1173. }
  1174. rule = PR_NEXT_LINK(rule);
  1175. }
  1176. }
  1177. }
  1178. /* Go through inclusive rules and build the target list. */
  1179. if (config->inclusive_rules) {
  1180. if (!PR_CLIST_IS_EMPTY((PRCList *)config->inclusive_rules)) {
  1181. /* Clear out our last hint from processing exclusions. */
  1182. last = NULL;
  1183. /* Get the first excluded target for exclusion checking. */
  1184. if (!PR_CLIST_IS_EMPTY(&exclusions)) {
  1185. curr_exclusion = PR_LIST_HEAD(&exclusions);
  1186. }
  1187. rule = PR_LIST_HEAD((PRCList *)config->inclusive_rules);
  1188. while (rule != (PRCList *)config->inclusive_rules) {
  1189. curr_rule = (struct automemberRegexRule *)rule;
  1190. /* The excluded targets are stored in alphabetical order. Instead
  1191. * of going through the entire exclusion list for each inclusive
  1192. * rule, we can simply go through the exclusion list once and keep
  1193. * track of our position. If the curent exclusion comes after
  1194. * the target DN used in the current inclusive rule, it can't be
  1195. * excluded. If the current exclusion comes before the target
  1196. * in the current rule, we need to go through the exclusion list
  1197. * until we find a target that is the same or comes after the
  1198. * current rule. */
  1199. if (curr_exclusion) {
  1200. while ((curr_exclusion != &exclusions) && (slapi_sdn_compare(
  1201. ((struct automemberDNListItem *)curr_exclusion)->dn,
  1202. curr_rule->target_group_dn) < 0)) {
  1203. curr_exclusion = PR_NEXT_LINK(curr_exclusion);
  1204. }
  1205. }
  1206. /* Regex rules are sorted by the target group DN. This means
  1207. * we can skip all rules for the last target group DN that we
  1208. * added to the targets list. We also skip any rules for
  1209. * target groups that have been excluded by an exclusion rule. */
  1210. if (((curr_exclusion == NULL) || (curr_exclusion == &exclusions) ||
  1211. slapi_sdn_compare(((struct automemberDNListItem *)curr_exclusion)->dn,
  1212. curr_rule->target_group_dn) != 0) && ((last == NULL) ||
  1213. (slapi_sdn_compare(last, curr_rule->target_group_dn) != 0))) {
  1214. /* Get comparison attr and loop through values. */
  1215. vals = slapi_entry_attr_get_charray(e, curr_rule->attr);
  1216. for (i = 0; vals && vals[i]; ++i) {
  1217. /* Evaluate the regex. */
  1218. if (slapi_re_exec(curr_rule->regex, vals[i], -1) == 1) {
  1219. /* Found a match. Add to the end of the targets list
  1220. * and set last as a hint to ourselves. */
  1221. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1222. "automember_update_membership: Adding \"%s\" "
  1223. "to list of target groups for \"%s\" "
  1224. "(matched: \"%s=%s\").\n",
  1225. slapi_sdn_get_dn(curr_rule->target_group_dn),
  1226. slapi_entry_get_dn(e), curr_rule->attr,
  1227. curr_rule->regex_str);
  1228. dnitem = (struct automemberDNListItem *)slapi_ch_calloc(1,
  1229. sizeof(struct automemberDNListItem));
  1230. /* We are just referencing the dn from the regex rule. We
  1231. * will not free it when we clean up this list. This list
  1232. * is more short-lived than the regex rule list, so we can
  1233. * get away with this optimization. */
  1234. dnitem->dn = curr_rule->target_group_dn;
  1235. PR_APPEND_LINK(&(dnitem->list), &targets);
  1236. last = curr_rule->target_group_dn;
  1237. }
  1238. }
  1239. slapi_ch_array_free(vals);
  1240. vals = NULL;
  1241. }
  1242. rule = PR_NEXT_LINK(rule);
  1243. }
  1244. }
  1245. }
  1246. /* If no targets, update default groups if set. Otherwise, update
  1247. * targets. Use a helper to do the actual updates. We can just pass an
  1248. * array of target group DNs along with our entry DN, the grouping attr,
  1249. * and the grouping value. */
  1250. if (PR_CLIST_IS_EMPTY(&targets)) {
  1251. /* Add to each default group. */
  1252. for (i = 0; config->default_groups && config->default_groups[i]; i++) {
  1253. automember_add_member_value(e, config->default_groups[i],
  1254. config->grouping_attr, config->grouping_value);
  1255. }
  1256. } else {
  1257. /* Update the target groups. */
  1258. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&targets);
  1259. while ((PRCList *)dnitem != &targets) {
  1260. automember_add_member_value(e, slapi_sdn_get_dn(dnitem->dn),
  1261. config->grouping_attr, config->grouping_value);
  1262. dnitem = (struct automemberDNListItem *)PR_NEXT_LINK((PRCList *)dnitem);
  1263. }
  1264. }
  1265. /* Free the exclusions and targets lists. Remember that the DN's
  1266. * are not ours, so don't free them! */
  1267. while (!PR_CLIST_IS_EMPTY(&exclusions)) {
  1268. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&exclusions);
  1269. PR_REMOVE_LINK((PRCList *)dnitem);
  1270. slapi_ch_free((void**)&dnitem);
  1271. }
  1272. while (!PR_CLIST_IS_EMPTY(&targets)) {
  1273. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&targets);
  1274. PR_REMOVE_LINK((PRCList *)dnitem);
  1275. slapi_ch_free((void**)&dnitem);
  1276. }
  1277. }
  1278. /*
  1279. * automember_add_member_value()
  1280. *
  1281. * Adds a member entry to a group.
  1282. */
  1283. static void
  1284. automember_add_member_value(Slapi_Entry *member_e, const char *group_dn,
  1285. char *grouping_attr, char *grouping_value)
  1286. {
  1287. Slapi_PBlock *mod_pb = slapi_pblock_new();
  1288. int result = LDAP_SUCCESS;
  1289. LDAPMod mod;
  1290. LDAPMod *mods[2];
  1291. char *vals[2];
  1292. char *member_value = NULL;
  1293. int freeit = 0;
  1294. /* If grouping_value is dn, we need to fetch the dn instead. */
  1295. if (slapi_attr_type_cmp(grouping_value, "dn", SLAPI_TYPE_CMP_EXACT) == 0) {
  1296. member_value = slapi_entry_get_ndn(member_e);
  1297. } else {
  1298. member_value = slapi_entry_attr_get_charptr(member_e, grouping_value);
  1299. freeit = 1;
  1300. }
  1301. if (member_value) {
  1302. /* Set up the operation. */
  1303. vals[0] = member_value;
  1304. vals[1] = 0;
  1305. mod.mod_op = LDAP_MOD_ADD;
  1306. mod.mod_type = grouping_attr;
  1307. mod.mod_values = vals;
  1308. mods[0] = &mod;
  1309. mods[1] = 0;
  1310. /* Perform the modify operation. */
  1311. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1312. "automember_add_member_value: Adding \"%s\" as "
  1313. "a \"%s\" value to group \"%s\".\n",
  1314. member_value, grouping_attr, group_dn);
  1315. slapi_modify_internal_set_pb(mod_pb, group_dn,
  1316. mods, 0, 0, automember_get_plugin_id(), 0);
  1317. slapi_modify_internal_pb(mod_pb);
  1318. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1319. if ((result != LDAP_SUCCESS) && (result != LDAP_TYPE_OR_VALUE_EXISTS)) {
  1320. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1321. "automember_add_member_value: Unable to add \"%s\" as "
  1322. "a \"%s\" value to group \"%s\" (%s).\n",
  1323. member_value, grouping_attr, group_dn,
  1324. ldap_err2string(result));
  1325. }
  1326. } else {
  1327. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1328. "automember_add_member_value: Unable to find grouping "
  1329. "value attribute \"%s\" in entry \"%s\".\n",
  1330. grouping_value, slapi_entry_get_dn(member_e));
  1331. }
  1332. /* Cleanup */
  1333. if (freeit) {
  1334. slapi_ch_free_string(&member_value);
  1335. }
  1336. slapi_pblock_destroy(mod_pb);
  1337. }
  1338. /*
  1339. * Operation callback functions
  1340. */
  1341. /*
  1342. * automember_pre_op()
  1343. *
  1344. * Checks if an operation affects the auto membership
  1345. * config and validates the config changes.
  1346. */
  1347. static int
  1348. automember_pre_op(Slapi_PBlock * pb, int modop)
  1349. {
  1350. Slapi_DN *sdn = 0;
  1351. Slapi_Entry *e = 0;
  1352. Slapi_Mods *smods = 0;
  1353. LDAPMod **mods;
  1354. int free_entry = 0;
  1355. char *errstr = NULL;
  1356. int ret = 0;
  1357. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1358. "--> automember_pre_op\n");
  1359. /* Just bail if we aren't ready to service requests yet. */
  1360. if (!g_plugin_started)
  1361. goto bail;
  1362. if (0 == (sdn = automember_get_sdn(pb)))
  1363. goto bail;
  1364. if (automember_dn_is_config(sdn)) {
  1365. /* Validate config changes, but don't apply them.
  1366. * This allows us to reject invalid config changes
  1367. * here at the pre-op stage. Applying the config
  1368. * needs to be done at the post-op stage. */
  1369. if (LDAP_CHANGETYPE_ADD == modop) {
  1370. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  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. } else if (LDAP_CHANGETYPE_MODIFY == modop) {
  1377. /* Fetch the entry being modified so we can
  1378. * create the resulting entry for validation. */
  1379. if (sdn) {
  1380. slapi_search_internal_get_entry(sdn, 0, &e, automember_get_plugin_id());
  1381. free_entry = 1;
  1382. }
  1383. /* If the entry doesn't exist, just bail and
  1384. * let the server handle it. */
  1385. if (e == NULL) {
  1386. goto bail;
  1387. }
  1388. /* Grab the mods. */
  1389. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1390. smods = slapi_mods_new();
  1391. slapi_mods_init_byref(smods, mods);
  1392. /* Apply the mods to create the resulting entry. */
  1393. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1394. /* The mods don't apply cleanly, so we just let this op go
  1395. * to let the main server handle it. */
  1396. goto bailmod;
  1397. }
  1398. } else {
  1399. errstr = slapi_ch_smprintf("automember_pre_op: invalid op type %d",
  1400. modop);
  1401. ret = LDAP_PARAM_ERROR;
  1402. goto bail;
  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_DN *sdn = NULL;
  1454. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1455. "--> automember_mod_post_op\n");
  1456. /* Just bail if we aren't ready to service requests yet. */
  1457. if (!g_plugin_started) {
  1458. goto bail;
  1459. }
  1460. if (automember_oktodo(pb) && (sdn = automember_get_sdn(pb))) {
  1461. /* Check if the config is being modified and reload if so. */
  1462. if (automember_dn_is_config(sdn)) {
  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. Slapi_DN *sdn = 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 ((sdn = automember_get_sdn(pb))) {
  1485. if (automember_dn_is_config(sdn)) {
  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. goto bail;
  1493. }
  1494. /* If replication, just bail. */
  1495. if (automember_isrepl(pb)) {
  1496. return 0;
  1497. }
  1498. /* Get the newly added entry. */
  1499. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1500. if (e) {
  1501. /* If the entry is a tombstone, just bail. */
  1502. Slapi_Value *tombstone =
  1503. slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
  1504. int rc = slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS,
  1505. tombstone);
  1506. slapi_value_free(&tombstone);
  1507. if (rc) {
  1508. return 0;
  1509. }
  1510. /* Check if a config entry applies
  1511. * to the entry being added. */
  1512. automember_config_read_lock();
  1513. /* Bail out if the plug-in close function was just called. */
  1514. if (!g_plugin_started) {
  1515. automember_config_unlock();
  1516. return 0;
  1517. }
  1518. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  1519. list = PR_LIST_HEAD(g_automember_config);
  1520. while (list != g_automember_config) {
  1521. config = (struct configEntry *)list;
  1522. /* Does the entry meet scope and filter requirements? */
  1523. if (slapi_dn_issuffix(slapi_sdn_get_dn(sdn), config->scope) &&
  1524. (slapi_filter_test_simple(e, config->filter) == 0)) {
  1525. /* Find out what membership changes are needed and make them. */
  1526. automember_update_membership(config, e);
  1527. }
  1528. list = PR_NEXT_LINK(list);
  1529. }
  1530. }
  1531. automember_config_unlock();
  1532. } else {
  1533. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1534. "automember_add_post_op: Error "
  1535. "retrieving post-op entry %s\n", slapi_sdn_get_dn(sdn));
  1536. }
  1537. bail:
  1538. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1539. "<-- automember_add_post_op\n");
  1540. return 0;
  1541. }
  1542. /*
  1543. * automember_del_post_op()
  1544. *
  1545. * Removes deleted config.
  1546. */
  1547. static int
  1548. automember_del_post_op(Slapi_PBlock *pb)
  1549. {
  1550. Slapi_DN *sdn = NULL;
  1551. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1552. "--> automember_del_post_op\n");
  1553. /* Just bail if we aren't ready to service requests yet. */
  1554. if (!g_plugin_started || !automember_oktodo(pb)) {
  1555. return 0;
  1556. }
  1557. /* Reload config if a config entry was deleted. */
  1558. if ((sdn = automember_get_sdn(pb))) {
  1559. if (automember_dn_is_config(sdn))
  1560. automember_load_config();
  1561. } else {
  1562. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1563. "automember_del_post_op: Error "
  1564. "retrieving dn\n");
  1565. }
  1566. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1567. "<-- automember_del_post_op\n");
  1568. return 0;
  1569. }
  1570. /*
  1571. * automember_modrdn_post_op()
  1572. *
  1573. * Reloads config if config entries move
  1574. * into or out of our config scope.
  1575. */
  1576. static int
  1577. automember_modrdn_post_op(Slapi_PBlock *pb)
  1578. {
  1579. Slapi_DN *old_sdn = NULL;
  1580. Slapi_DN *new_sdn = NULL;
  1581. Slapi_Entry *post_e = NULL;
  1582. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1583. "--> automember_modrdn_post_op\n");
  1584. /* Just bail if we aren't ready to service requests yet. */
  1585. if (!g_plugin_started || !automember_oktodo(pb))
  1586. return 0;
  1587. /* Reload config if an existing config entry was renamed,
  1588. * or if the new dn brings an entry into the scope of the
  1589. * config entries. */
  1590. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  1591. if (post_e) {
  1592. new_sdn = slapi_entry_get_sdn(post_e);
  1593. } else {
  1594. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1595. "automember_modrdn_post_op: Error "
  1596. "retrieving post-op entry\n");
  1597. return 0;
  1598. }
  1599. if ((old_sdn = automember_get_sdn(pb))) {
  1600. if (automember_dn_is_config(old_sdn) || automember_dn_is_config(new_sdn))
  1601. automember_load_config();
  1602. } else {
  1603. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1604. "automember_modrdn_post_op: Error "
  1605. "retrieving dn\n");
  1606. }
  1607. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1608. "<-- automember_modrdn_post_op\n");
  1609. return 0;
  1610. }