automember.c 65 KB

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