automember.c 66 KB

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