automember.c 65 KB

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