1
0

automember.c 63 KB

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