automember.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. /*
  12. * Auto Membership Plug-in
  13. */
  14. #include "automember.h"
  15. /*
  16. * Plug-in globals
  17. */
  18. static PRCList *g_automember_config = NULL;
  19. static Slapi_RWLock *g_automember_config_lock = NULL;
  20. static void *_PluginID = NULL;
  21. static Slapi_DN *_PluginDN = NULL;
  22. static Slapi_DN *_ConfigAreaDN = NULL;
  23. static Slapi_PluginDesc pdesc = { AUTOMEMBER_FEATURE_DESC,
  24. VENDOR,
  25. DS_PACKAGE_VERSION,
  26. AUTOMEMBER_PLUGIN_DESC };
  27. /*
  28. * Plug-in management functions
  29. */
  30. int automember_init(Slapi_PBlock * pb);
  31. static int automember_start(Slapi_PBlock * pb);
  32. static int automember_close(Slapi_PBlock * pb);
  33. static int automember_postop_init(Slapi_PBlock * pb);
  34. static int automember_internal_postop_init(Slapi_PBlock *pb);
  35. /*
  36. * Operation callbacks (where the real work is done)
  37. */
  38. static int automember_mod_post_op(Slapi_PBlock *pb);
  39. static int automember_add_post_op(Slapi_PBlock *pb);
  40. static int automember_del_post_op(Slapi_PBlock *pb);
  41. static int automember_modrdn_post_op(Slapi_PBlock *pb);
  42. static int automember_pre_op(Slapi_PBlock *pb, int modop);
  43. static int automember_mod_pre_op(Slapi_PBlock *pb);
  44. static int automember_add_pre_op(Slapi_PBlock *pb);
  45. /*
  46. * Config cache management functions
  47. */
  48. static int automember_load_config(void);
  49. static void automember_delete_config(void);
  50. static int automember_parse_config_entry(Slapi_Entry * e, int apply);
  51. static void automember_free_config_entry(struct configEntry ** entry);
  52. /*
  53. * helpers
  54. */
  55. static Slapi_DN *automember_get_sdn(Slapi_PBlock *pb);
  56. static Slapi_DN *automember_get_config_area(void);
  57. static void automember_set_config_area(Slapi_DN *sdn);
  58. static int automember_dn_is_config(Slapi_DN *sdn);
  59. static int automember_oktodo(Slapi_PBlock *pb);
  60. static int automember_isrepl(Slapi_PBlock *pb);
  61. static void automember_parse_regex_entry(struct configEntry *config, Slapi_Entry *e);
  62. static struct automemberRegexRule *automember_parse_regex_rule(char *rule_string);
  63. static void automember_free_regex_rule(struct automemberRegexRule *rule);
  64. static int automember_parse_grouping_attr(char *value, char **grouping_attr,
  65. char **grouping_value);
  66. static int automember_update_membership(struct configEntry *config, Slapi_Entry *e, PRFileDesc *ldif_fd);
  67. static int automember_add_member_value(Slapi_Entry *member_e, const char *group_dn,
  68. char *grouping_attr, char *grouping_value, PRFileDesc *ldif_fd);
  69. const char *fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val);
  70. /*
  71. * task functions
  72. */
  73. static int automember_task_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  74. int *returncode, char *returntext, void *arg);
  75. static int automember_task_add_export_updates(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  76. int *returncode, char *returntext, void *arg);
  77. static int automember_task_add_map_entries(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  78. int *returncode, char *returntext, void *arg);
  79. void automember_rebuild_task_thread(void *arg);
  80. void automember_export_task_thread(void *arg);
  81. void automember_map_task_thread(void *arg);
  82. static void automember_task_destructor(Slapi_Task *task);
  83. static void automember_task_export_destructor(Slapi_Task *task);
  84. static void automember_task_map_destructor(Slapi_Task *task);
  85. #define DEFAULT_FILE_MODE PR_IRUSR | PR_IWUSR
  86. /*
  87. * Config cache locking functions
  88. */
  89. void
  90. automember_config_read_lock()
  91. {
  92. slapi_rwlock_rdlock(g_automember_config_lock);
  93. }
  94. void
  95. automember_config_write_lock()
  96. {
  97. slapi_rwlock_wrlock(g_automember_config_lock);
  98. }
  99. void
  100. automember_config_unlock()
  101. {
  102. slapi_rwlock_unlock(g_automember_config_lock);
  103. }
  104. /*
  105. * Plugin identity functions
  106. */
  107. void
  108. automember_set_plugin_id(void *pluginID)
  109. {
  110. _PluginID = pluginID;
  111. }
  112. void *
  113. automember_get_plugin_id()
  114. {
  115. return _PluginID;
  116. }
  117. void
  118. automember_set_plugin_sdn(Slapi_DN *pluginDN)
  119. {
  120. _PluginDN = pluginDN;
  121. }
  122. Slapi_DN *
  123. automember_get_plugin_sdn(void)
  124. {
  125. return _PluginDN;
  126. }
  127. static int plugin_is_betxn = 0;
  128. /*
  129. * Plug-in initialization functions
  130. */
  131. int
  132. automember_init(Slapi_PBlock *pb)
  133. {
  134. int status = 0;
  135. char *plugin_identity = NULL;
  136. Slapi_Entry *plugin_entry = NULL;
  137. char *plugin_type = NULL;
  138. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  139. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  140. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  141. "--> automember_init\n");
  142. /* get args */
  143. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  144. plugin_entry &&
  145. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  146. plugin_type && strstr(plugin_type, "betxn")) {
  147. plugin_is_betxn = 1;
  148. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  149. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  150. }
  151. slapi_ch_free_string(&plugin_type);
  152. /* Store the plugin identity for later use.
  153. * Used for internal operations. */
  154. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  155. PR_ASSERT(plugin_identity);
  156. automember_set_plugin_id(plugin_identity);
  157. /* Register callbacks */
  158. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  159. SLAPI_PLUGIN_VERSION_01) != 0 ||
  160. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  161. (void *) automember_start) != 0 ||
  162. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  163. (void *) automember_close) != 0 ||
  164. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  165. (void *) &pdesc) != 0 ||
  166. slapi_pblock_set(pb, premod, (void *) automember_mod_pre_op) != 0 ||
  167. slapi_pblock_set(pb, preadd, (void *) automember_add_pre_op) != 0) {
  168. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  169. "automember_init - Failed to register plugin\n");
  170. status = -1;
  171. }
  172. if (!plugin_is_betxn && !status &&
  173. slapi_register_plugin("internalpostoperation", /* op type */
  174. 1, /* Enabled */
  175. "automember_init", /* this function desc */
  176. automember_internal_postop_init, /* init func */
  177. AUTOMEMBER_INT_POSTOP_DESC, /* plugin desc */
  178. NULL, /* ? */
  179. plugin_identity /* access control */
  180. )) {
  181. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  182. "automember_init - Failed to register internalpostoperation plugin\n");
  183. status = -1;
  184. }
  185. if (!status) {
  186. plugin_type = "postoperation";
  187. if (plugin_is_betxn) {
  188. plugin_type = "betxnpostoperation";
  189. }
  190. if (slapi_register_plugin(plugin_type, /* op type */
  191. 1, /* Enabled */
  192. "automember_init", /* this function desc */
  193. automember_postop_init, /* init func for post op */
  194. AUTOMEMBER_POSTOP_DESC, /* plugin desc */
  195. NULL, /* ? */
  196. plugin_identity /* access control */
  197. )
  198. ) {
  199. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  200. "automember_init - Failed to register postop plugin\n");
  201. status = -1;
  202. }
  203. }
  204. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  205. "<-- automember_init\n");
  206. return status;
  207. }
  208. /* not used when using plugin as a betxn plugin - betxn plugins are called for both internal and external ops */
  209. static int
  210. automember_internal_postop_init(Slapi_PBlock *pb)
  211. {
  212. int status = 0;
  213. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  214. SLAPI_PLUGIN_VERSION_01) != 0 ||
  215. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  216. (void *) &pdesc) != 0 ||
  217. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  218. (void *) automember_add_post_op) != 0 ||
  219. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  220. (void *) automember_del_post_op) != 0 ||
  221. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  222. (void *) automember_mod_post_op) != 0 ||
  223. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  224. (void *) automember_modrdn_post_op) != 0) {
  225. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  226. "automember_internal_postop_init - Failed to register plugin\n");
  227. status = -1;
  228. }
  229. return status;
  230. }
  231. static int
  232. automember_postop_init(Slapi_PBlock *pb)
  233. {
  234. int status = 0;
  235. int addfn = SLAPI_PLUGIN_POST_ADD_FN;
  236. int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
  237. int modfn = SLAPI_PLUGIN_POST_MODIFY_FN;
  238. int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
  239. if (plugin_is_betxn) {
  240. addfn = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  241. delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  242. modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  243. mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  244. }
  245. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  246. SLAPI_PLUGIN_VERSION_01) != 0 ||
  247. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  248. (void *) &pdesc) != 0 ||
  249. slapi_pblock_set(pb, addfn, (void *) automember_add_post_op) != 0 ||
  250. slapi_pblock_set(pb, delfn, (void *) automember_del_post_op) != 0 ||
  251. slapi_pblock_set(pb, modfn, (void *) automember_mod_post_op) != 0 ||
  252. slapi_pblock_set(pb, mdnfn, (void *) automember_modrdn_post_op) != 0) {
  253. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  254. "automember_postop_init - Failed to register plugin\n");
  255. status = -1;
  256. }
  257. return status;
  258. }
  259. /* Stash the config area in the pblock for start functions? */
  260. /*
  261. * automember_start()
  262. *
  263. * Creates config lock and loads config cache.
  264. */
  265. static int
  266. automember_start(Slapi_PBlock * pb)
  267. {
  268. Slapi_DN *plugindn = NULL;
  269. char *config_area = NULL;
  270. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  271. "--> automember_start\n");
  272. slapi_plugin_task_register_handler("automember rebuild membership", automember_task_add, pb);
  273. slapi_plugin_task_register_handler("automember export updates", automember_task_add_export_updates, pb);
  274. slapi_plugin_task_register_handler("automember map updates", automember_task_add_map_entries, pb);
  275. if ((g_automember_config_lock = slapi_new_rwlock()) == NULL) {
  276. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  277. "automember_start - Lock creation failed\n");
  278. return -1;
  279. }
  280. /*
  281. * Get the plug-in target dn from the system
  282. * and store it for future use. */
  283. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &plugindn);
  284. if (NULL == plugindn || 0 == slapi_sdn_get_ndn_len(plugindn)) {
  285. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  286. "automember_start - Unable to retrieve plugin dn\n");
  287. return -1;
  288. }
  289. automember_set_plugin_sdn(slapi_sdn_dup(plugindn));
  290. /* Set the alternate config area if one is defined. */
  291. slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
  292. if (config_area) {
  293. automember_set_config_area(slapi_sdn_new_normdn_byval(config_area));
  294. }
  295. /*
  296. * Load the config cache
  297. */
  298. g_automember_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  299. PR_INIT_CLIST(g_automember_config);
  300. if (automember_load_config() != 0) {
  301. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  302. "automember_start - Unable to load plug-in configuration\n");
  303. return -1;
  304. }
  305. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  306. "auto membership plug-in: ready for service\n");
  307. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  308. "<-- automember_start\n");
  309. return 0;
  310. }
  311. /*
  312. * automember_close()
  313. *
  314. * Cleans up the config cache.
  315. */
  316. static int
  317. automember_close(Slapi_PBlock * pb)
  318. {
  319. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  320. "--> automember_close\n");
  321. /* unregister the tasks */
  322. slapi_plugin_task_unregister_handler("automember rebuild membership",
  323. automember_task_add);
  324. slapi_plugin_task_unregister_handler("automember export updates",
  325. automember_task_add_export_updates);
  326. slapi_plugin_task_unregister_handler("automember map updates",
  327. automember_task_add_map_entries);
  328. automember_delete_config();
  329. slapi_sdn_free(&_PluginDN);
  330. slapi_sdn_free(&_ConfigAreaDN);
  331. slapi_destroy_rwlock(g_automember_config_lock);
  332. g_automember_config_lock = NULL;
  333. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  334. "<-- automember_close\n");
  335. return 0;
  336. }
  337. /*
  338. * automember_get_config()
  339. *
  340. * Get the config list.
  341. */
  342. PRCList *
  343. automember_get_config()
  344. {
  345. return g_automember_config;
  346. }
  347. /*
  348. * automember_load_config()
  349. *
  350. * Parse and load the config entries.
  351. */
  352. static int
  353. automember_load_config(void)
  354. {
  355. int status = 0;
  356. int result;
  357. int i;
  358. Slapi_PBlock *search_pb;
  359. Slapi_Entry **entries = NULL;
  360. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  361. "--> automember_load_config\n");
  362. /* Clear out any old config. */
  363. automember_config_write_lock();
  364. automember_delete_config();
  365. g_automember_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  366. PR_INIT_CLIST(g_automember_config);
  367. search_pb = slapi_pblock_new();
  368. /* If an alternate config area is configured, find
  369. * the config entries that are beneath it, otherwise
  370. * we load the entries beneath our top-level plug-in
  371. * config entry. */
  372. if (automember_get_config_area()) {
  373. /* Find the config entries beneath the alternate config area. */
  374. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  375. "automember_load_config - Looking for config entries "
  376. "beneath \"%s\".\n", slapi_sdn_get_ndn(automember_get_config_area()));
  377. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(automember_get_config_area()),
  378. LDAP_SCOPE_SUBTREE, AUTOMEMBER_DEFINITION_FILTER,
  379. NULL, 0, NULL, NULL, automember_get_plugin_id(), 0);
  380. } else {
  381. /* Find the config entries beneath our plugin entry. */
  382. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  383. "automember_load_config - Looking for config entries "
  384. "beneath \"%s\".\n", slapi_sdn_get_ndn(automember_get_plugin_sdn()));
  385. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(automember_get_plugin_sdn()),
  386. LDAP_SCOPE_SUBTREE, AUTOMEMBER_DEFINITION_FILTER,
  387. NULL, 0, NULL, NULL, automember_get_plugin_id(), 0);
  388. }
  389. slapi_search_internal_pb(search_pb);
  390. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  391. if (LDAP_SUCCESS != result) {
  392. if (automember_get_config_area() && (result == LDAP_NO_SUCH_OBJECT)) {
  393. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  394. "automember_load_config - Config container \"%s\" does "
  395. "not exist.\n", slapi_sdn_get_ndn(automember_get_config_area()));
  396. goto cleanup;
  397. } else {
  398. status = -1;
  399. goto cleanup;
  400. }
  401. }
  402. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  403. &entries);
  404. /* Loop through all of the entries we found and parse them. */
  405. for (i = 0; entries && (entries[i] != NULL); i++) {
  406. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  407. "automember_load_config - Parsing config entry "
  408. "\"%s\".\n", slapi_entry_get_dn(entries[i]));
  409. /* We don't care about the status here because we may have
  410. * some invalid config entries, but we just want to continue
  411. * looking for valid ones. */
  412. automember_parse_config_entry(entries[i], 1);
  413. }
  414. cleanup:
  415. slapi_free_search_results_internal(search_pb);
  416. slapi_pblock_destroy(search_pb);
  417. automember_config_unlock();
  418. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  419. "<-- automember_load_config\n");
  420. return status;
  421. }
  422. /*
  423. * automember_parse_config_entry()
  424. *
  425. * Parses a single config entry. If apply is non-zero, then
  426. * we will load and start using the new config. You can simply
  427. * validate config without making any changes by setting apply
  428. * to 0.
  429. *
  430. * Returns 0 if the entry is valid and -1 if it is invalid.
  431. */
  432. static int
  433. automember_parse_config_entry(Slapi_Entry * e, int apply)
  434. {
  435. char *value = NULL;
  436. char **values = NULL;
  437. struct configEntry *entry = NULL;
  438. struct configEntry *config_entry;
  439. PRCList *list;
  440. Slapi_PBlock *search_pb = NULL;
  441. Slapi_Entry **rule_entries = NULL;
  442. char *filter_str = NULL;
  443. Slapi_DN *dn = NULL;
  444. Slapi_Filter *filter = NULL;
  445. int result;
  446. int entry_added = 0;
  447. int i = 0;
  448. int ret = 0;
  449. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  450. "--> automember_parse_config_entry\n");
  451. /* If this is the main plug-in config entry or
  452. * the config area container, just bail. */
  453. if ((slapi_sdn_compare(automember_get_plugin_sdn(), slapi_entry_get_sdn(e)) == 0) ||
  454. (automember_get_config_area() && (slapi_sdn_compare(automember_get_config_area(),
  455. slapi_entry_get_sdn(e)) == 0))) {
  456. goto bail;
  457. }
  458. /* If this entry is not an automember config definition entry, just bail. */
  459. filter_str = slapi_ch_strdup(AUTOMEMBER_DEFINITION_FILTER);
  460. filter = slapi_str2filter(filter_str);
  461. if (slapi_filter_test_simple(e, filter) != 0) {
  462. goto bail;
  463. }
  464. /* If marked as disabled, just bail. */
  465. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_DISABLED_TYPE);
  466. if (value) {
  467. slapi_ch_free_string(&value);
  468. goto bail;
  469. }
  470. entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  471. if (NULL == entry) {
  472. ret = -1;
  473. goto bail;
  474. }
  475. value = slapi_entry_get_ndn(e);
  476. if (value) {
  477. entry->dn = slapi_ch_strdup(value);
  478. } else {
  479. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  480. "automember_parse_config_entry - Error "
  481. "reading dn from config entry\n");
  482. ret = -1;
  483. goto bail;
  484. }
  485. slapi_log_err(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  486. "----------> dn [%s]\n", entry->dn);
  487. /* Load the scope */
  488. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_SCOPE_TYPE);
  489. if (value) {
  490. entry->scope = value;
  491. } else {
  492. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  493. "automember_parse_config_entry - The %s config "
  494. "setting is required for config entry \"%s\".\n",
  495. AUTOMEMBER_SCOPE_TYPE, entry->dn);
  496. ret = -1;
  497. goto bail;
  498. }
  499. /* Load the filter */
  500. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_FILTER_TYPE);
  501. if (value) {
  502. /* Convert to a Slapi_Filter to improve performance. */
  503. if (NULL == (entry->filter = slapi_str2filter(value))) {
  504. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM ,
  505. "automember_parse_config_entry - Invalid search filter in "
  506. "%s config setting for config entry \"%s\" "
  507. "(filter = \"%s\").\n", AUTOMEMBER_FILTER_TYPE, entry->dn, value);
  508. ret = -1;
  509. }
  510. slapi_ch_free_string(&value);
  511. if (ret != 0) {
  512. goto bail;
  513. }
  514. } else {
  515. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  516. "automember_parse_config_entry - The %s config "
  517. "setting is required for config entry \"%s\".\n",
  518. AUTOMEMBER_FILTER_TYPE, entry->dn);
  519. ret = -1;
  520. goto bail;
  521. }
  522. /* Load the default groups */
  523. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_DEFAULT_GROUP_TYPE);
  524. if (values) {
  525. /* Just hand off the values */
  526. entry->default_groups = values;
  527. /*
  528. * If we set the config area, we need to make sure that the default groups are not
  529. * in the config area, or else we could deadlock on updates.
  530. */
  531. if(automember_get_config_area()){
  532. for(i = 0; values && values[i]; i++){
  533. dn = slapi_sdn_new_dn_byref(values[i]);
  534. if(slapi_sdn_issuffix(dn, automember_get_config_area())){
  535. /* The groups are under the config area - not good */
  536. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  537. "automember_parse_config_entry - The default group \"%s\" can not be "
  538. "a child of the plugin config area \"%s\".\n",
  539. values[i], slapi_sdn_get_dn(automember_get_config_area()));
  540. slapi_sdn_free(&dn);
  541. ret = -1;
  542. goto bail;
  543. }
  544. slapi_sdn_free(&dn);
  545. }
  546. }
  547. values = NULL;
  548. }
  549. /* Load the grouping attr */
  550. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_GROUPING_ATTR_TYPE);
  551. if (value) {
  552. if (automember_parse_grouping_attr(value, &(entry->grouping_attr),
  553. &(entry->grouping_value)) != 0) {
  554. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  555. "automember_parse_config_entry - Invalid "
  556. "%s config setting for config entry \"%s\" "
  557. "(value: \"%s\").\n", AUTOMEMBER_GROUPING_ATTR_TYPE,
  558. entry->dn, value);
  559. ret = -1;
  560. }
  561. slapi_ch_free_string(&value);
  562. if (ret != 0) {
  563. goto bail;
  564. }
  565. } else {
  566. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  567. "automember_parse_config_entry - The %s config "
  568. "setting is required for config entry \"%s\".\n",
  569. AUTOMEMBER_GROUPING_ATTR_TYPE, entry->dn);
  570. ret = -1;
  571. goto bail;
  572. }
  573. /* Find all child regex rule entries */
  574. search_pb = slapi_pblock_new();
  575. slapi_search_internal_set_pb(search_pb, entry->dn, LDAP_SCOPE_SUBTREE,
  576. AUTOMEMBER_REGEX_RULE_FILTER, NULL, 0, NULL,
  577. NULL, automember_get_plugin_id(), 0);
  578. slapi_search_internal_pb(search_pb);
  579. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  580. /* If this is a new config entry being added, it won't exist yet
  581. * when we are simply validating config. We can just ignore no
  582. * such object errors. */
  583. if ((LDAP_SUCCESS != result) && (LDAP_NO_SUCH_OBJECT != result)) {
  584. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  585. "automember_parse_config_entry - Error searching "
  586. "for child rule entries for config \"%s\" (err=%d).",
  587. entry->dn, result);
  588. ret = -1;
  589. goto bail;
  590. }
  591. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  592. &rule_entries);
  593. /* Go through each child rule entry and parse it. */
  594. for (i = 0; rule_entries && (rule_entries[i] != NULL); i++) {
  595. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  596. "automember_parse_config_entry - Parsing regex rule entry "
  597. "\"%s\".\n", slapi_entry_get_dn(rule_entries[i]));
  598. automember_parse_regex_entry(entry, rule_entries[i]);
  599. }
  600. /* If we were only called to validate config, we can
  601. * just bail out before applying the config changes */
  602. if (apply == 0) {
  603. goto bail;
  604. }
  605. /* Add the config object to the list. We order by scope. */
  606. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  607. list = PR_LIST_HEAD(g_automember_config);
  608. while (list != g_automember_config) {
  609. config_entry = (struct configEntry *) list;
  610. /* If the config entry we are adding has a scope that is
  611. * a child of the scope of the current list item, we insert
  612. * the entry before that list item. */
  613. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  614. PR_INSERT_BEFORE(&(entry->list), list);
  615. slapi_log_err(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  616. "automember_parse_config_entry - store [%s] before [%s] \n", entry->dn,
  617. config_entry->dn);
  618. entry_added = 1;
  619. break;
  620. }
  621. list = PR_NEXT_LINK(list);
  622. /* If we hit the end of the list, add to the tail. */
  623. if (g_automember_config == list) {
  624. PR_INSERT_BEFORE(&(entry->list), list);
  625. slapi_log_err(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  626. "automember_parse_config_entry - store [%s] at tail\n", entry->dn);
  627. entry_added = 1;
  628. break;
  629. }
  630. }
  631. } else {
  632. /* first entry */
  633. PR_INSERT_LINK(&(entry->list), g_automember_config);
  634. slapi_log_err(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  635. "automember_parse_config_entry - store [%s] at head \n", entry->dn);
  636. entry_added = 1;
  637. }
  638. bail:
  639. if (0 == entry_added) {
  640. /* Don't log error if we weren't asked to apply config */
  641. if ((apply != 0) && (entry != NULL)) {
  642. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  643. "automember_parse_config_entry - Invalid config entry "
  644. "[%s] skipped\n", entry->dn);
  645. }
  646. automember_free_config_entry(&entry);
  647. } else {
  648. ret = 0;
  649. }
  650. slapi_ch_free_string(&filter_str);
  651. slapi_filter_free(filter, 1);
  652. slapi_free_search_results_internal(search_pb);
  653. slapi_pblock_destroy(search_pb);
  654. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  655. "<-- automember_parse_config_entry\n");
  656. return ret;
  657. }
  658. static void
  659. automember_free_config_entry(struct configEntry ** entry)
  660. {
  661. struct configEntry *e = *entry;
  662. if (e == NULL)
  663. return;
  664. if (e->dn) {
  665. slapi_log_err(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  666. "automember_free_config_entry - Freeing config entry [%s]\n", e->dn);
  667. slapi_ch_free_string(&e->dn);
  668. }
  669. if (e->scope) {
  670. slapi_ch_free_string(&e->scope);
  671. }
  672. if (e->filter) {
  673. slapi_filter_free(e->filter, 1);
  674. }
  675. if (e->exclusive_rules) {
  676. PRCList *list;
  677. /* Clear out the list contents. */
  678. while (!PR_CLIST_IS_EMPTY((PRCList *)e->exclusive_rules)) {
  679. list = PR_LIST_HEAD((PRCList *)e->exclusive_rules);
  680. PR_REMOVE_LINK(list);
  681. automember_free_regex_rule((struct automemberRegexRule *)list);
  682. }
  683. /* Free the list itself. */
  684. slapi_ch_free((void **)&(e->exclusive_rules));
  685. }
  686. /* Clear out the list contents. */
  687. if (e->inclusive_rules) {
  688. PRCList *list;
  689. while (!PR_CLIST_IS_EMPTY((PRCList *)e->inclusive_rules)) {
  690. list = PR_LIST_HEAD((PRCList *)e->inclusive_rules);
  691. PR_REMOVE_LINK(list);
  692. automember_free_regex_rule((struct automemberRegexRule *)list);
  693. }
  694. /* Free the list itself. */
  695. slapi_ch_free((void **)&(e->inclusive_rules));
  696. }
  697. if (e->default_groups) {
  698. slapi_ch_array_free(e->default_groups);
  699. }
  700. if (e->grouping_attr) {
  701. slapi_ch_free_string(&e->grouping_attr);
  702. }
  703. if (e->grouping_value) {
  704. slapi_ch_free_string(&e->grouping_value);
  705. }
  706. slapi_ch_free((void **)entry);
  707. }
  708. static void
  709. automember_delete_configEntry(PRCList *entry)
  710. {
  711. PR_REMOVE_LINK(entry);
  712. automember_free_config_entry((struct configEntry **) &entry);
  713. }
  714. static void
  715. automember_delete_config(void)
  716. {
  717. PRCList *list;
  718. /* Delete the config cache. */
  719. while (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  720. list = PR_LIST_HEAD(g_automember_config);
  721. automember_delete_configEntry(list);
  722. }
  723. slapi_ch_free((void **)&g_automember_config);
  724. return;
  725. }
  726. static Slapi_DN *
  727. automember_get_sdn(Slapi_PBlock * pb)
  728. {
  729. Slapi_DN *sdn = 0;
  730. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  731. "--> automember_get_sdn\n");
  732. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  733. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  734. "<-- automember_get_sdn\n");
  735. return sdn;
  736. }
  737. void
  738. automember_set_config_area(Slapi_DN *sdn)
  739. {
  740. _ConfigAreaDN = sdn;
  741. }
  742. Slapi_DN *
  743. automember_get_config_area(void)
  744. {
  745. return _ConfigAreaDN;
  746. }
  747. /*
  748. * automember_dn_is_config()
  749. *
  750. * Checks if dn is an auto membership config entry.
  751. */
  752. static int
  753. automember_dn_is_config(Slapi_DN *sdn)
  754. {
  755. int ret = 0;
  756. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  757. "--> automember_dn_is_config\n");
  758. if (sdn == NULL) {
  759. goto bail;
  760. }
  761. /* If an alternate config area is configured, treat it's child
  762. * entries as config entries. If the alternate config area is
  763. * not configured, treat children of the top-level plug-in
  764. * config entry as our config entries. */
  765. if (automember_get_config_area()) {
  766. if (slapi_sdn_issuffix(sdn, automember_get_config_area()) &&
  767. slapi_sdn_compare(sdn, automember_get_config_area())) {
  768. ret = 1;
  769. }
  770. } else {
  771. if (slapi_sdn_issuffix(sdn, automember_get_plugin_sdn()) &&
  772. slapi_sdn_compare(sdn, automember_get_plugin_sdn())) {
  773. ret = 1;
  774. }
  775. }
  776. bail:
  777. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  778. "<-- automember_dn_is_config\n");
  779. return ret;
  780. }
  781. /*
  782. * automember_oktodo()
  783. *
  784. * Check if we want to process this operation. We need to be
  785. * sure that the operation succeeded.
  786. */
  787. static int
  788. automember_oktodo(Slapi_PBlock *pb)
  789. {
  790. int ret = 1;
  791. int oprc = 0;
  792. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  793. "--> automember_oktodo\n" );
  794. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0) {
  795. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  796. "automember_oktodo - Could not get parameters\n" );
  797. ret = -1;
  798. }
  799. /* This plugin should only execute if the operation succeeded. */
  800. if(oprc != 0) {
  801. ret = 0;
  802. }
  803. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  804. "<-- automember_oktodo\n" );
  805. return ret;
  806. }
  807. /*
  808. * automember_isrepl()
  809. *
  810. * Returns 1 if the operation associated with pb
  811. * is a replicated op. Returns 0 otherwise.
  812. */
  813. static int
  814. automember_isrepl(Slapi_PBlock *pb)
  815. {
  816. int is_repl = 0;
  817. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  818. "--> automember_isrepl\n" );
  819. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  820. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  821. "<-- automember_isrepl\n" );
  822. return is_repl;
  823. }
  824. /*
  825. * automember_parse_regex_entry()
  826. *
  827. * Parses a rule entry and adds the regex rules to the
  828. * passed in config struct. Invalid regex rules will
  829. * be skipped and logged at the fatal log level.
  830. */
  831. static void
  832. automember_parse_regex_entry(struct configEntry *config, Slapi_Entry *e)
  833. {
  834. char *target_group = NULL;
  835. char **values = NULL;
  836. Slapi_DN *group_dn = NULL;
  837. PRCList *list;
  838. int i = 0;
  839. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  840. "--> automember_parse_regex_entry\n");
  841. /* Make sure the target group was specified. */
  842. target_group = slapi_entry_attr_get_charptr(e, AUTOMEMBER_TARGET_GROUP_TYPE);
  843. if (!target_group) {
  844. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  845. "automember_parse_regex_entry - The %s config "
  846. "setting is required for rule entry \"%s\".\n",
  847. AUTOMEMBER_TARGET_GROUP_TYPE, slapi_entry_get_ndn(e));
  848. goto bail;
  849. }
  850. /* Ensure that the target group DN is valid. */
  851. if (slapi_dn_syntax_check(NULL, target_group, 1) != 0) {
  852. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  853. "automember_parse_regex_entry - Invalid target group DN "
  854. "in rule \"%s\" (dn=\"%s\").\n", slapi_entry_get_ndn(e),
  855. target_group);
  856. goto bail;
  857. }
  858. /* normalize the group dn and compare it to the configArea DN */
  859. if(automember_get_config_area()){
  860. group_dn = slapi_sdn_new_dn_byref(target_group);
  861. if(slapi_sdn_issuffix(group_dn, automember_get_config_area())){
  862. /* The target group is under the plugin config area - not good */
  863. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  864. "automember_parse_regex_entry - The target group \"%s\" can not be "
  865. "a child of the plugin config area \"%s\".\n",
  866. slapi_sdn_get_dn(group_dn), slapi_sdn_get_dn(automember_get_config_area()));
  867. slapi_sdn_free(&group_dn);
  868. goto bail;
  869. }
  870. slapi_sdn_free(&group_dn);
  871. }
  872. /* Load inclusive rules */
  873. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_INC_REGEX_TYPE);
  874. if (values) {
  875. struct automemberRegexRule *rule = NULL;
  876. /* If we haven't loaded any inclusive rules for
  877. * this config definition yet, create a new list. */
  878. if (config->inclusive_rules == NULL) {
  879. /* Create a list to hold our regex rules */
  880. config->inclusive_rules = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  881. PR_INIT_CLIST((PRCList *)config->inclusive_rules);
  882. }
  883. for (i = 0; values && values[i]; ++i) {
  884. rule = automember_parse_regex_rule(values[i]);
  885. if (rule) {
  886. /* Fill in the target group. */
  887. rule->target_group_dn = slapi_sdn_new_normdn_byval(target_group);
  888. if (!PR_CLIST_IS_EMPTY((PRCList *)config->inclusive_rules)) {
  889. list = PR_LIST_HEAD((PRCList *)config->inclusive_rules);
  890. while (list != (PRCList *)config->inclusive_rules) {
  891. struct automemberRegexRule *curr_rule = (struct automemberRegexRule *)list;
  892. /* Order rules by target group DN */
  893. if (slapi_sdn_compare(rule->target_group_dn, curr_rule->target_group_dn) < 0) {
  894. PR_INSERT_BEFORE(&(rule->list), list);
  895. break;
  896. }
  897. list = PR_NEXT_LINK(list);
  898. /* If we hit the end of the list, add to the tail. */
  899. if ((PRCList *)config->inclusive_rules == list) {
  900. PR_INSERT_BEFORE(&(rule->list), list);
  901. break;
  902. }
  903. }
  904. } else {
  905. /* Add to head of list */
  906. PR_INSERT_LINK(&(rule->list), (PRCList *)config->inclusive_rules);
  907. }
  908. } else {
  909. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  910. "automember_parse_regex_entry - Skipping invalid inclusive "
  911. "regex rule in rule entry \"%s\" (rule = \"%s\").\n",
  912. slapi_entry_get_ndn(e), values[i]);
  913. }
  914. }
  915. slapi_ch_array_free(values);
  916. values = NULL;
  917. }
  918. /* Load exclusive rules. */
  919. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_EXC_REGEX_TYPE);
  920. if (values) {
  921. struct automemberRegexRule *rule = NULL;
  922. /* If we haven't loaded any exclusive rules for
  923. * this config definition yet, create a new list. */
  924. if (config->exclusive_rules == NULL) {
  925. /* Create a list to hold our regex rules */
  926. config->exclusive_rules = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  927. PR_INIT_CLIST((PRCList *)config->exclusive_rules);
  928. }
  929. for (i = 0; values && values[i]; ++i) {
  930. rule = automember_parse_regex_rule(values[i]);
  931. if (rule) {
  932. /* Fill in the target group. */
  933. rule->target_group_dn = slapi_sdn_new_normdn_byval(target_group);
  934. if (!PR_CLIST_IS_EMPTY((PRCList *)config->exclusive_rules)) {
  935. list = PR_LIST_HEAD((PRCList *)config->exclusive_rules);
  936. while (list != (PRCList *)config->exclusive_rules) {
  937. struct automemberRegexRule *curr_rule = (struct automemberRegexRule *)list;
  938. /* Order rules by target group DN */
  939. if (slapi_sdn_compare(rule->target_group_dn, curr_rule->target_group_dn) < 0) {
  940. PR_INSERT_BEFORE(&(rule->list), list);
  941. break;
  942. }
  943. list = PR_NEXT_LINK(list);
  944. /* If we hit the end of the list, add to the tail. */
  945. if ((PRCList *)config->exclusive_rules == list) {
  946. PR_INSERT_BEFORE(&(rule->list), list);
  947. break;
  948. }
  949. }
  950. } else {
  951. /* Add to head of list */
  952. PR_INSERT_LINK(&(rule->list), (PRCList *)config->exclusive_rules);
  953. }
  954. } else {
  955. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  956. "automember_parse_regex_entry - Skipping invalid exclusive "
  957. "regex rule in rule entry \"%s\" (rule = \"%s\").\n",
  958. slapi_entry_get_ndn(e), values[i]);
  959. }
  960. }
  961. slapi_ch_array_free(values);
  962. values = NULL;
  963. }
  964. bail:
  965. slapi_ch_free_string(&target_group);
  966. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  967. "<-- automember_parse_regex_entry\n");
  968. }
  969. /*
  970. * automember_parse_regex_rule()
  971. *
  972. * Parses a regex rule and returns a regex rule struct. The caller
  973. * will need to free this struct when it is finished with it. If
  974. * there is a problem parsing the regex rule, an error will be
  975. * logged and NULL will be returned.
  976. */
  977. static struct automemberRegexRule *
  978. automember_parse_regex_rule(char *rule_string)
  979. {
  980. struct automemberRegexRule *rule = NULL;
  981. char *attr = NULL;
  982. Slapi_Regex *regex = NULL;
  983. const char *recomp_result = NULL;
  984. char *p = NULL;
  985. char *p2 = NULL;
  986. /* A rule is in the form "attr=regex". */
  987. /* Find the comparison attribute name. */
  988. if ((p = strchr(rule_string, '=')) == NULL) {
  989. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  990. "automember_parse_regex_rule - Unable to parse "
  991. "regex rule (missing '=' delimeter).\n");
  992. goto bail;
  993. }
  994. /* Make sure the attribute name is not empty. */
  995. if (p == rule_string) {
  996. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  997. "automember_parse_regex_rule - Unable to parse "
  998. " regex rule (missing comparison attribute).\n");
  999. goto bail;
  1000. }
  1001. if ((attr = strndup(rule_string, p - rule_string)) == NULL) {
  1002. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1003. "automember_parse_regex_rule - Unable to allocate "
  1004. "memory.\n");
  1005. goto bail;
  1006. }
  1007. /* Validate the attribute. */
  1008. for (p2 = attr; p2 && (*p2 != '\0'); p2++) {
  1009. if (!IS_ATTRDESC_CHAR(*p2)) {
  1010. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1011. "automember_parse_regex_rule - Invalid comparison "
  1012. "attribute name \"%s\".\n", attr);
  1013. goto bail;
  1014. }
  1015. }
  1016. /* Find the regex. */
  1017. p++;
  1018. if (*p == '\0') {
  1019. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1020. "automember_parse_regex_rule - Unable to parse "
  1021. "regex rule (missing regex).\n");
  1022. goto bail;
  1023. }
  1024. /* Compile the regex to validate it. */
  1025. regex = slapi_re_comp(p, &recomp_result);
  1026. if (!regex) {
  1027. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1028. "automember_parse_regex_rule - Unable to parse "
  1029. "regex rule (invalid regex). Error \"%s\".\n",
  1030. recomp_result?recomp_result:"unknown");
  1031. }
  1032. /* Validation has passed, so create the regex rule struct and fill it in.
  1033. * We hand off everything we have allocated. All of this will be free'd
  1034. * when the rule struct itself is freed. */
  1035. rule = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  1036. rule->attr = attr;
  1037. rule->regex_str = slapi_ch_strdup(p);
  1038. rule->regex = regex;
  1039. bail:
  1040. /* Cleanup if we didn't successfully create a rule. */
  1041. if (!rule) {
  1042. slapi_ch_free_string(&attr);
  1043. slapi_re_free(regex);
  1044. }
  1045. return rule;
  1046. }
  1047. /*
  1048. * automember_free_regex_rule()
  1049. *
  1050. * Frees a regex rule and all of it's contents from memory.
  1051. */
  1052. static void
  1053. automember_free_regex_rule(struct automemberRegexRule *rule)
  1054. {
  1055. if (rule) {
  1056. if (rule->target_group_dn) {
  1057. slapi_sdn_free(&(rule->target_group_dn));
  1058. }
  1059. if (rule->attr) {
  1060. slapi_ch_free_string(&(rule->attr));
  1061. }
  1062. if (rule->regex_str) {
  1063. slapi_ch_free_string(&(rule->regex_str));
  1064. }
  1065. if (rule->regex) {
  1066. slapi_re_free(rule->regex);
  1067. }
  1068. }
  1069. slapi_ch_free((void **)&rule);
  1070. }
  1071. /*
  1072. * automember_parse_grouping_attr()
  1073. *
  1074. * Parses a grouping attribute and grouping value from
  1075. * the passed in config string. Memory will be allocated
  1076. * for grouping_attr and grouping_value, so it is up to
  1077. * the called to free them when they are no longer needed.
  1078. * Returns 0 upon success and 1 upon failure.
  1079. */
  1080. static int
  1081. automember_parse_grouping_attr(char *value, char **grouping_attr, char **grouping_value)
  1082. {
  1083. int ret = 0;
  1084. char *p = NULL;
  1085. /* Clear out any existing type or value. */
  1086. slapi_ch_free_string(grouping_attr);
  1087. slapi_ch_free_string(grouping_value);
  1088. /* split out the type from the value (use the first ':') */
  1089. if ((p = strchr(value, ':')) == NULL) {
  1090. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1091. "automember_parse_grouping_attr - Value for grouping attribute "
  1092. "is not in the correct format. (value: \"%s\").\n", value);
  1093. ret = 1;
  1094. goto bail;
  1095. }
  1096. /* Ensure the type is not empty. */
  1097. if (p == value) {
  1098. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1099. "automember_parse_grouping_attr - Value for grouping attribute "
  1100. "is not in the correct format. The grouping attribute is missing. "
  1101. "(value: \"%s\").\n", value);
  1102. ret = 1;
  1103. goto bail;
  1104. }
  1105. /* Duplicate the type to be returned. */
  1106. *grouping_attr = strndup(value, p - value);
  1107. /* Advance p to point to the beginning of the value. */
  1108. p++;
  1109. while (*p == ' ') {
  1110. p++;
  1111. }
  1112. /* Ensure the value is not empty. */
  1113. if (*p == '\0') {
  1114. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1115. "automember_parse_grouping_attr - Value for grouping attribute "
  1116. "is not in the correct format. The grouping value is "
  1117. "missing. (value: \"%s\").\n", value);
  1118. ret = 1;
  1119. goto bail;
  1120. }
  1121. /* Duplicate the value to be returned. */
  1122. *grouping_value = slapi_ch_strdup(p);
  1123. /* Ensure that memory was allocated successfully. */
  1124. if ((*grouping_attr == NULL) || (*grouping_value == NULL)) {
  1125. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1126. "automember_parse_grouping_attr - Error allocating memory.\n");
  1127. ret = 1;
  1128. goto bail;
  1129. }
  1130. /* Ensure that the grouping attr is a legal attr name. */
  1131. for (p = *grouping_attr; p && (*p != '\0'); p++) {
  1132. if (!IS_ATTRDESC_CHAR(*p)) {
  1133. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1134. "automember_parse_grouping_attr: Invalid value for "
  1135. "grouping attribute. The grouping attribute type is "
  1136. "illegal. (type: \"%s\").\n", *grouping_attr);
  1137. ret = 1;
  1138. goto bail;
  1139. }
  1140. }
  1141. /* Ensure that the grouping value type is a legal attr name. */
  1142. for (p = *grouping_value; p && (*p != '\0'); p++) {
  1143. if (!IS_ATTRDESC_CHAR(*p)) {
  1144. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1145. "automember_parse_grouping_attr - Invalid value for "
  1146. "grouping attribute. The grouping value type is "
  1147. "illegal. (type: \"%s\").\n", *grouping_value);
  1148. ret = 1;
  1149. goto bail;
  1150. }
  1151. }
  1152. bail:
  1153. if (ret != 0) {
  1154. slapi_ch_free_string(grouping_attr);
  1155. slapi_ch_free_string(grouping_value);
  1156. }
  1157. return ret;
  1158. }
  1159. /*
  1160. * automember_update_membership()
  1161. *
  1162. * Determines which target groups need to be updated according to
  1163. * the rules in config, then performs the updates.
  1164. */
  1165. static int
  1166. automember_update_membership(struct configEntry *config, Slapi_Entry *e, PRFileDesc *ldif_fd)
  1167. {
  1168. PRCList *rule = NULL;
  1169. struct automemberRegexRule *curr_rule = NULL;
  1170. PRCList exclusions;
  1171. PRCList targets;
  1172. struct automemberDNListItem *dnitem = NULL;
  1173. Slapi_DN *last = NULL;
  1174. PRCList *curr_exclusion = NULL;
  1175. char **vals = NULL;
  1176. int rc = 0;
  1177. int i = 0;
  1178. if (!config || !e) {
  1179. return -1;
  1180. }
  1181. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1182. "automember_update_membership - Processing \"%s\" "
  1183. "definition entry for candidate entry \"%s\".\n",
  1184. config->dn, slapi_entry_get_dn(e));
  1185. /* Initialize our lists that keep track of targets. */
  1186. PR_INIT_CLIST(&exclusions);
  1187. PR_INIT_CLIST(&targets);
  1188. /* Go through exclusive rules and build an exclusion list. */
  1189. if (config->exclusive_rules) {
  1190. if (!PR_CLIST_IS_EMPTY((PRCList *)config->exclusive_rules)) {
  1191. rule = PR_LIST_HEAD((PRCList *)config->exclusive_rules);
  1192. while (rule != (PRCList *)config->exclusive_rules) {
  1193. curr_rule = (struct automemberRegexRule *)rule;
  1194. /* Regex rules are sorted by the target group DN. This means
  1195. * we can skip all rules for the last target group DN that we
  1196. * added to the exclusions list. */
  1197. if ((last == NULL) || slapi_sdn_compare(last, curr_rule->target_group_dn) != 0) {
  1198. /* Get comparison attr and loop through values. */
  1199. vals = slapi_entry_attr_get_charray(e, curr_rule->attr);
  1200. for (i = 0; vals && vals[i]; ++i) {
  1201. /* Evaluate the regex. */
  1202. if (slapi_re_exec(curr_rule->regex, vals[i], -1) == 1) {
  1203. /* Found a match. Add to end of the exclusion list
  1204. * and set last as a hint to ourselves. */
  1205. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1206. "automember_update_membership - Adding \"%s\" "
  1207. "to list of excluded groups for \"%s\" "
  1208. "(matched: \"%s=%s\").\n",
  1209. slapi_sdn_get_dn(curr_rule->target_group_dn),
  1210. slapi_entry_get_dn(e), curr_rule->attr,
  1211. curr_rule->regex_str);
  1212. dnitem = (struct automemberDNListItem *)slapi_ch_calloc(1,
  1213. sizeof(struct automemberDNListItem));
  1214. /* We are just referencing the dn from the regex rule. We
  1215. * will not free it when we clean up this list. This list
  1216. * is more short-lived than the regex rule list, so we can
  1217. * get away with this optimization. */
  1218. dnitem->dn = curr_rule->target_group_dn;
  1219. PR_APPEND_LINK(&(dnitem->list), &exclusions);
  1220. last = curr_rule->target_group_dn;
  1221. }
  1222. }
  1223. slapi_ch_array_free(vals);
  1224. vals = NULL;
  1225. }
  1226. rule = PR_NEXT_LINK(rule);
  1227. }
  1228. }
  1229. }
  1230. /* Go through inclusive rules and build the target list. */
  1231. if (config->inclusive_rules) {
  1232. if (!PR_CLIST_IS_EMPTY((PRCList *)config->inclusive_rules)) {
  1233. /* Clear out our last hint from processing exclusions. */
  1234. last = NULL;
  1235. /* Get the first excluded target for exclusion checking. */
  1236. if (!PR_CLIST_IS_EMPTY(&exclusions)) {
  1237. curr_exclusion = PR_LIST_HEAD(&exclusions);
  1238. }
  1239. rule = PR_LIST_HEAD((PRCList *)config->inclusive_rules);
  1240. while (rule != (PRCList *)config->inclusive_rules) {
  1241. curr_rule = (struct automemberRegexRule *)rule;
  1242. /* The excluded targets are stored in alphabetical order. Instead
  1243. * of going through the entire exclusion list for each inclusive
  1244. * rule, we can simply go through the exclusion list once and keep
  1245. * track of our position. If the curent exclusion comes after
  1246. * the target DN used in the current inclusive rule, it can't be
  1247. * excluded. If the current exclusion comes before the target
  1248. * in the current rule, we need to go through the exclusion list
  1249. * until we find a target that is the same or comes after the
  1250. * current rule. */
  1251. if (curr_exclusion) {
  1252. while ((curr_exclusion != &exclusions) && (slapi_sdn_compare(
  1253. ((struct automemberDNListItem *)curr_exclusion)->dn,
  1254. curr_rule->target_group_dn) < 0)) {
  1255. curr_exclusion = PR_NEXT_LINK(curr_exclusion);
  1256. }
  1257. }
  1258. /* Regex rules are sorted by the target group DN. This means
  1259. * we can skip all rules for the last target group DN that we
  1260. * added to the targets list. We also skip any rules for
  1261. * target groups that have been excluded by an exclusion rule. */
  1262. if (((curr_exclusion == NULL) || (curr_exclusion == &exclusions) ||
  1263. slapi_sdn_compare(((struct automemberDNListItem *)curr_exclusion)->dn,
  1264. curr_rule->target_group_dn) != 0) && ((last == NULL) ||
  1265. (slapi_sdn_compare(last, curr_rule->target_group_dn) != 0))) {
  1266. /* Get comparison attr and loop through values. */
  1267. vals = slapi_entry_attr_get_charray(e, curr_rule->attr);
  1268. for (i = 0; vals && vals[i]; ++i) {
  1269. /* Evaluate the regex. */
  1270. if (slapi_re_exec(curr_rule->regex, vals[i], -1) == 1) {
  1271. /* Found a match. Add to the end of the targets list
  1272. * and set last as a hint to ourselves. */
  1273. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1274. "automember_update_membership - Adding \"%s\" "
  1275. "to list of target groups for \"%s\" "
  1276. "(matched: \"%s=%s\").\n",
  1277. slapi_sdn_get_dn(curr_rule->target_group_dn),
  1278. slapi_entry_get_dn(e), curr_rule->attr,
  1279. curr_rule->regex_str);
  1280. dnitem = (struct automemberDNListItem *)slapi_ch_calloc(1,
  1281. sizeof(struct automemberDNListItem));
  1282. /* We are just referencing the dn from the regex rule. We
  1283. * will not free it when we clean up this list. This list
  1284. * is more short-lived than the regex rule list, so we can
  1285. * get away with this optimization. */
  1286. dnitem->dn = curr_rule->target_group_dn;
  1287. PR_APPEND_LINK(&(dnitem->list), &targets);
  1288. last = curr_rule->target_group_dn;
  1289. }
  1290. }
  1291. slapi_ch_array_free(vals);
  1292. vals = NULL;
  1293. }
  1294. rule = PR_NEXT_LINK(rule);
  1295. }
  1296. }
  1297. }
  1298. /* If no targets, update default groups if set. Otherwise, update
  1299. * targets. Use a helper to do the actual updates. We can just pass an
  1300. * array of target group DNs along with our entry DN, the grouping attr,
  1301. * and the grouping value. */
  1302. if (PR_CLIST_IS_EMPTY(&targets)) {
  1303. /* Add to each default group. */
  1304. for (i = 0; config->default_groups && config->default_groups[i]; i++) {
  1305. if(automember_add_member_value(e, config->default_groups[i], config->grouping_attr,
  1306. config->grouping_value, ldif_fd))
  1307. {
  1308. rc = SLAPI_PLUGIN_FAILURE;
  1309. goto out;
  1310. }
  1311. }
  1312. } else {
  1313. /* Update the target groups. */
  1314. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&targets);
  1315. while ((PRCList *)dnitem != &targets) {
  1316. if(automember_add_member_value(e, slapi_sdn_get_dn(dnitem->dn),config->grouping_attr,
  1317. config->grouping_value, ldif_fd))
  1318. {
  1319. rc = SLAPI_PLUGIN_FAILURE;
  1320. goto out;
  1321. }
  1322. dnitem = (struct automemberDNListItem *)PR_NEXT_LINK((PRCList *)dnitem);
  1323. }
  1324. }
  1325. /* Free the exclusions and targets lists. Remember that the DN's
  1326. * are not ours, so don't free them! */
  1327. while (!PR_CLIST_IS_EMPTY(&exclusions)) {
  1328. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&exclusions);
  1329. PR_REMOVE_LINK((PRCList *)dnitem);
  1330. slapi_ch_free((void**)&dnitem);
  1331. }
  1332. while (!PR_CLIST_IS_EMPTY(&targets)) {
  1333. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&targets);
  1334. PR_REMOVE_LINK((PRCList *)dnitem);
  1335. slapi_ch_free((void**)&dnitem);
  1336. }
  1337. out:
  1338. return rc;
  1339. }
  1340. /*
  1341. * automember_add_member_value()
  1342. *
  1343. * Adds a member entry to a group.
  1344. */
  1345. static int
  1346. automember_add_member_value(Slapi_Entry *member_e, const char *group_dn, char *grouping_attr,
  1347. char *grouping_value, PRFileDesc *ldif_fd)
  1348. {
  1349. Slapi_PBlock *mod_pb = slapi_pblock_new();
  1350. int result = LDAP_SUCCESS;
  1351. LDAPMod mod;
  1352. LDAPMod *mods[2];
  1353. char *vals[2];
  1354. char *member_value = NULL;
  1355. int freeit = 0;
  1356. int rc = 0;
  1357. /* If grouping_value is dn, we need to fetch the dn instead. */
  1358. if (slapi_attr_type_cmp(grouping_value, "dn", SLAPI_TYPE_CMP_EXACT) == 0) {
  1359. member_value = slapi_entry_get_ndn(member_e);
  1360. } else {
  1361. member_value = slapi_entry_attr_get_charptr(member_e, grouping_value);
  1362. freeit = 1;
  1363. }
  1364. /*
  1365. * If ldif_fd is set, we are performing an export task. Write the changes to the
  1366. * file instead of performing them
  1367. */
  1368. if(ldif_fd){
  1369. PR_fprintf(ldif_fd, "dn: %s\n", group_dn);
  1370. PR_fprintf(ldif_fd, "changetype: modify\n");
  1371. PR_fprintf(ldif_fd, "add: %s\n", grouping_attr);
  1372. PR_fprintf(ldif_fd, "%s: %s\n", grouping_attr, member_value);
  1373. PR_fprintf(ldif_fd, "\n");
  1374. goto out;
  1375. }
  1376. if (member_value) {
  1377. /* Set up the operation. */
  1378. vals[0] = member_value;
  1379. vals[1] = 0;
  1380. mod.mod_op = LDAP_MOD_ADD;
  1381. mod.mod_type = grouping_attr;
  1382. mod.mod_values = vals;
  1383. mods[0] = &mod;
  1384. mods[1] = 0;
  1385. /* Perform the modify operation. */
  1386. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1387. "automember_add_member_value - Adding \"%s\" as "
  1388. "a \"%s\" value to group \"%s\".\n",
  1389. member_value, grouping_attr, group_dn);
  1390. slapi_modify_internal_set_pb(mod_pb, group_dn,
  1391. mods, 0, 0, automember_get_plugin_id(), 0);
  1392. slapi_modify_internal_pb(mod_pb);
  1393. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1394. if ((result != LDAP_SUCCESS) && (result != LDAP_TYPE_OR_VALUE_EXISTS)) {
  1395. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1396. "automember_add_member_value - Unable to add \"%s\" as "
  1397. "a \"%s\" value to group \"%s\" (%s).\n",
  1398. member_value, grouping_attr, group_dn,
  1399. ldap_err2string(result));
  1400. rc = result;
  1401. }
  1402. } else {
  1403. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1404. "automember_add_member_value - Unable to find grouping "
  1405. "value attribute \"%s\" in entry \"%s\".\n",
  1406. grouping_value, slapi_entry_get_dn(member_e));
  1407. }
  1408. out:
  1409. /* Cleanup */
  1410. if (freeit) {
  1411. slapi_ch_free_string(&member_value);
  1412. }
  1413. slapi_pblock_destroy(mod_pb);
  1414. return rc;
  1415. }
  1416. /*
  1417. * Operation callback functions
  1418. */
  1419. /*
  1420. * automember_pre_op()
  1421. *
  1422. * Checks if an operation affects the auto membership
  1423. * config and validates the config changes.
  1424. */
  1425. static int
  1426. automember_pre_op(Slapi_PBlock * pb, int modop)
  1427. {
  1428. Slapi_DN *sdn = 0;
  1429. Slapi_Entry *e = 0;
  1430. Slapi_Mods *smods = 0;
  1431. LDAPMod **mods;
  1432. int free_entry = 0;
  1433. char *errstr = NULL;
  1434. int ret = SLAPI_PLUGIN_SUCCESS;
  1435. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1436. "--> automember_pre_op\n");
  1437. if (0 == (sdn = automember_get_sdn(pb)))
  1438. goto bail;
  1439. if (automember_dn_is_config(sdn)) {
  1440. /* Validate config changes, but don't apply them.
  1441. * This allows us to reject invalid config changes
  1442. * here at the pre-op stage. Applying the config
  1443. * needs to be done at the post-op stage. */
  1444. if (LDAP_CHANGETYPE_ADD == modop) {
  1445. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  1446. /* If the entry doesn't exist, just bail and
  1447. * let the server handle it. */
  1448. if (e == NULL) {
  1449. goto bail;
  1450. }
  1451. } else if (LDAP_CHANGETYPE_MODIFY == modop) {
  1452. /* Fetch the entry being modified so we can
  1453. * create the resulting entry for validation. */
  1454. if (sdn) {
  1455. slapi_search_internal_get_entry(sdn, 0, &e, automember_get_plugin_id());
  1456. free_entry = 1;
  1457. }
  1458. /* If the entry doesn't exist, just bail and
  1459. * let the server handle it. */
  1460. if (e == NULL) {
  1461. goto bail;
  1462. }
  1463. /* Grab the mods. */
  1464. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1465. smods = slapi_mods_new();
  1466. slapi_mods_init_byref(smods, mods);
  1467. /* Apply the mods to create the resulting entry. */
  1468. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1469. /* The mods don't apply cleanly, so we just let this op go
  1470. * to let the main server handle it. */
  1471. goto bailmod;
  1472. }
  1473. } else {
  1474. errstr = slapi_ch_smprintf("automember_pre_op - Invalid op type %d",
  1475. modop);
  1476. ret = LDAP_PARAM_ERROR;
  1477. goto bail;
  1478. }
  1479. if (automember_parse_config_entry(e, 0) != 0) {
  1480. /* Refuse the operation if config parsing failed. */
  1481. ret = LDAP_UNWILLING_TO_PERFORM;
  1482. if (LDAP_CHANGETYPE_ADD == modop) {
  1483. errstr = slapi_ch_smprintf("Not a valid auto membership configuration entry.");
  1484. } else {
  1485. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1486. "auto membership configuration.");
  1487. }
  1488. }
  1489. }
  1490. bailmod:
  1491. /* Clean up smods. */
  1492. if (LDAP_CHANGETYPE_MODIFY == modop) {
  1493. slapi_mods_free(&smods);
  1494. }
  1495. bail:
  1496. if (free_entry && e)
  1497. slapi_entry_free(e);
  1498. if (ret) {
  1499. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1500. "automember_pre_op - Operation failure [%d]\n", ret);
  1501. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  1502. slapi_ch_free((void **)&errstr);
  1503. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  1504. ret = SLAPI_PLUGIN_FAILURE;
  1505. }
  1506. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1507. "<-- automember_pre_op\n");
  1508. return ret;
  1509. }
  1510. static int
  1511. automember_add_pre_op(Slapi_PBlock * pb)
  1512. {
  1513. return automember_pre_op(pb, LDAP_CHANGETYPE_ADD);
  1514. }
  1515. static int
  1516. automember_mod_pre_op(Slapi_PBlock * pb)
  1517. {
  1518. return automember_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  1519. }
  1520. /*
  1521. * automember_mod_post_op()
  1522. *
  1523. * Reloads the auto membership config
  1524. * if config changes were made.
  1525. */
  1526. static int
  1527. automember_mod_post_op(Slapi_PBlock *pb)
  1528. {
  1529. Slapi_DN *sdn = NULL;
  1530. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1531. "--> automember_mod_post_op\n");
  1532. if (automember_oktodo(pb) && (sdn = automember_get_sdn(pb))) {
  1533. /* Check if the config is being modified and reload if so. */
  1534. if (automember_dn_is_config(sdn)) {
  1535. automember_load_config();
  1536. }
  1537. }
  1538. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1539. "<-- automember_mod_post_op\n");
  1540. return SLAPI_PLUGIN_SUCCESS;
  1541. }
  1542. static int
  1543. automember_add_post_op(Slapi_PBlock *pb)
  1544. {
  1545. Slapi_Entry *e = NULL;
  1546. Slapi_DN *sdn = NULL;
  1547. struct configEntry *config = NULL;
  1548. PRCList *list = NULL;
  1549. int rc = SLAPI_PLUGIN_SUCCESS;
  1550. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1551. "--> automember_add_post_op\n");
  1552. /* Reload config if a config entry was added. */
  1553. if ((sdn = automember_get_sdn(pb))) {
  1554. if (automember_dn_is_config(sdn)) {
  1555. automember_load_config();
  1556. }
  1557. } else {
  1558. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1559. "automember_add_post_op - Error retrieving dn\n");
  1560. rc = SLAPI_PLUGIN_FAILURE;
  1561. goto bail;
  1562. }
  1563. /* If replication, just bail. */
  1564. if (automember_isrepl(pb)) {
  1565. return SLAPI_PLUGIN_SUCCESS;
  1566. }
  1567. /* Get the newly added entry. */
  1568. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1569. if (e) {
  1570. /* If the entry is a tombstone, just bail. */
  1571. Slapi_Value *tombstone = slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
  1572. int is_tombstone = slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS,
  1573. tombstone);
  1574. slapi_value_free(&tombstone);
  1575. if (is_tombstone) {
  1576. return SLAPI_PLUGIN_SUCCESS;
  1577. }
  1578. /* Check if a config entry applies
  1579. * to the entry being added. */
  1580. automember_config_read_lock();
  1581. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  1582. list = PR_LIST_HEAD(g_automember_config);
  1583. while (list != g_automember_config) {
  1584. config = (struct configEntry *)list;
  1585. /* Does the entry meet scope and filter requirements? */
  1586. if (slapi_dn_issuffix(slapi_sdn_get_dn(sdn), config->scope) &&
  1587. (slapi_filter_test_simple(e, config->filter) == 0)) {
  1588. /* Find out what membership changes are needed and make them. */
  1589. if(automember_update_membership(config, e, NULL)){
  1590. rc = SLAPI_PLUGIN_FAILURE;
  1591. break;
  1592. }
  1593. }
  1594. list = PR_NEXT_LINK(list);
  1595. }
  1596. }
  1597. automember_config_unlock();
  1598. } else {
  1599. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1600. "automember_add_post_op - Error "
  1601. "retrieving post-op entry %s\n", slapi_sdn_get_dn(sdn));
  1602. }
  1603. bail:
  1604. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1605. "<-- automember_add_post_op (%d)\n", rc);
  1606. if(rc){
  1607. char errtxt[SLAPI_DSE_RETURNTEXT_SIZE];
  1608. int result = LDAP_UNWILLING_TO_PERFORM;
  1609. PR_snprintf(errtxt, SLAPI_DSE_RETURNTEXT_SIZE, "Automember Plugin update unexpectedly failed.\n");
  1610. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &result);
  1611. slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, &errtxt);
  1612. }
  1613. return rc;
  1614. }
  1615. /*
  1616. * automember_del_post_op()
  1617. *
  1618. * Removes deleted config.
  1619. */
  1620. static int
  1621. automember_del_post_op(Slapi_PBlock *pb)
  1622. {
  1623. Slapi_DN *sdn = NULL;
  1624. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1625. "--> automember_del_post_op\n");
  1626. /* Reload config if a config entry was deleted. */
  1627. if ((sdn = automember_get_sdn(pb))) {
  1628. if (automember_dn_is_config(sdn))
  1629. automember_load_config();
  1630. } else {
  1631. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1632. "automember_del_post_op - Error retrieving dn\n");
  1633. }
  1634. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1635. "<-- automember_del_post_op\n");
  1636. return SLAPI_PLUGIN_SUCCESS;
  1637. }
  1638. typedef struct _task_data
  1639. {
  1640. char *filter_str;
  1641. char *ldif_out;
  1642. char *ldif_in;
  1643. Slapi_DN *base_dn;
  1644. char *bind_dn;
  1645. int scope;
  1646. } task_data;
  1647. /*
  1648. * extract a single value from the entry (as a string) -- if it's not in the
  1649. * entry, the default will be returned (which can be NULL).
  1650. * you do not need to free anything returned by this.
  1651. */
  1652. const char *
  1653. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  1654. {
  1655. Slapi_Value *val = NULL;
  1656. Slapi_Attr *attr;
  1657. if(slapi_entry_attr_find(e, attrname, &attr) != 0){
  1658. return default_val;
  1659. }
  1660. slapi_attr_first_value(attr, &val);
  1661. return slapi_value_get_string(val);
  1662. }
  1663. static void
  1664. automember_task_destructor(Slapi_Task *task)
  1665. {
  1666. if (task) {
  1667. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1668. while (slapi_task_get_refcount(task) > 0) {
  1669. /* Yield to wait for the fixup task finishes. */
  1670. DS_Sleep (PR_MillisecondsToInterval(100));
  1671. }
  1672. if (mydata) {
  1673. slapi_ch_free_string(&mydata->bind_dn);
  1674. slapi_sdn_free(&mydata->base_dn);
  1675. slapi_ch_free_string(&mydata->filter_str);
  1676. slapi_ch_free((void **)&mydata);
  1677. }
  1678. }
  1679. }
  1680. static void
  1681. automember_task_export_destructor(Slapi_Task *task)
  1682. {
  1683. if (task) {
  1684. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1685. while (slapi_task_get_refcount(task) > 0) {
  1686. /* Yield to wait for the fixup task finishes. */
  1687. DS_Sleep (PR_MillisecondsToInterval(100));
  1688. }
  1689. if (mydata) {
  1690. slapi_ch_free_string(&mydata->ldif_out);
  1691. slapi_ch_free_string(&mydata->bind_dn);
  1692. slapi_sdn_free(&mydata->base_dn);
  1693. slapi_ch_free_string(&mydata->filter_str);
  1694. slapi_ch_free((void **)&mydata);
  1695. }
  1696. }
  1697. }
  1698. static void
  1699. automember_task_map_destructor(Slapi_Task *task)
  1700. {
  1701. if (task) {
  1702. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1703. while (slapi_task_get_refcount(task) > 0) {
  1704. /* Yield to wait for the fixup task finishes. */
  1705. DS_Sleep (PR_MillisecondsToInterval(100));
  1706. }
  1707. if (mydata) {
  1708. slapi_ch_free_string(&mydata->ldif_out);
  1709. slapi_ch_free_string(&mydata->ldif_in);
  1710. slapi_ch_free_string(&mydata->bind_dn);
  1711. slapi_ch_free((void **)&mydata);
  1712. }
  1713. }
  1714. }
  1715. /*
  1716. * automember_task_add
  1717. *
  1718. * This task is designed to "retro-fit" entries that existed prior to
  1719. * enabling this plugin. This can be an expensive task to run, but it's
  1720. * better than processing every modify operation in an attempt to catch
  1721. * entries that have not been processed.
  1722. *
  1723. * task entry:
  1724. *
  1725. * dn: cn=my rebuild task, cn=automember rebuild membership,cn=tasks,cn=config
  1726. * objectClass: top
  1727. * objectClass: extensibleObject
  1728. * cn: my rebuild task
  1729. * basedn: dc=example,dc=com
  1730. * filter: (uid=*)
  1731. * scope: sub
  1732. *
  1733. * basedn and filter are required. If scope is omitted, the default is sub
  1734. */
  1735. static int
  1736. automember_task_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1737. int *returncode, char *returntext, void *arg)
  1738. {
  1739. int rv = SLAPI_DSE_CALLBACK_OK;
  1740. task_data *mytaskdata = NULL;
  1741. Slapi_Task *task = NULL;
  1742. PRThread *thread = NULL;
  1743. char *bind_dn = NULL;
  1744. const char *base_dn;
  1745. const char *filter;
  1746. const char *scope;
  1747. *returncode = LDAP_SUCCESS;
  1748. /*
  1749. * Grab the task params
  1750. */
  1751. if((base_dn = fetch_attr(e, "basedn", 0)) == NULL){
  1752. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1753. rv = SLAPI_DSE_CALLBACK_ERROR;
  1754. goto out;
  1755. }
  1756. if((filter = fetch_attr(e, "filter", 0)) == NULL){
  1757. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1758. rv = SLAPI_DSE_CALLBACK_ERROR;
  1759. goto out;
  1760. }
  1761. scope = fetch_attr(e, "scope", "sub");
  1762. /*
  1763. * setup our task data
  1764. */
  1765. mytaskdata = (task_data*)slapi_ch_calloc(1, sizeof(task_data));
  1766. if (mytaskdata == NULL){
  1767. *returncode = LDAP_OPERATIONS_ERROR;
  1768. rv = SLAPI_DSE_CALLBACK_ERROR;
  1769. goto out;
  1770. }
  1771. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  1772. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  1773. mytaskdata->base_dn = slapi_sdn_new_dn_byval(base_dn);
  1774. mytaskdata->filter_str = slapi_ch_strdup(filter);
  1775. if(scope){
  1776. if(strcasecmp(scope,"sub")== 0){
  1777. mytaskdata->scope = 2;
  1778. } else if(strcasecmp(scope,"one")== 0){
  1779. mytaskdata->scope = 1;
  1780. } else if(strcasecmp(scope,"base")== 0){
  1781. mytaskdata->scope = 0;
  1782. } else {
  1783. /* Hmm, possible typo, use subtree */
  1784. mytaskdata->scope = 2;
  1785. }
  1786. } else {
  1787. /* subtree by default */
  1788. mytaskdata->scope = 2;
  1789. }
  1790. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  1791. slapi_task_set_destructor_fn(task, automember_task_destructor);
  1792. slapi_task_set_data(task, mytaskdata);
  1793. /*
  1794. * Start the task as a separate thread
  1795. */
  1796. thread = PR_CreateThread(PR_USER_THREAD, automember_rebuild_task_thread,
  1797. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1798. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1799. if (thread == NULL){
  1800. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1801. "automember_task_add - Unable to create task thread!\n");
  1802. *returncode = LDAP_OPERATIONS_ERROR;
  1803. slapi_task_finish(task, *returncode);
  1804. rv = SLAPI_DSE_CALLBACK_ERROR;
  1805. } else {
  1806. rv = SLAPI_DSE_CALLBACK_OK;
  1807. }
  1808. out:
  1809. return rv;
  1810. }
  1811. /*
  1812. * automember_rebuild_task_thread()
  1813. *
  1814. * Search using the basedn, filter, and scope provided from the task data.
  1815. * Then loop of each entry, and apply the membership if applicable.
  1816. */
  1817. void automember_rebuild_task_thread(void *arg)
  1818. {
  1819. Slapi_Task *task = (Slapi_Task *)arg;
  1820. struct configEntry *config = NULL;
  1821. Slapi_PBlock *search_pb = NULL, *fixup_pb = NULL;
  1822. Slapi_Entry **entries = NULL;
  1823. task_data *td = NULL;
  1824. PRCList *list = NULL;
  1825. int result = 0;
  1826. int i = 0;
  1827. if (!task) {
  1828. return; /* no task */
  1829. }
  1830. slapi_task_inc_refcount(task);
  1831. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1832. "automember_rebuild_task_thread - Refcount incremented.\n" );
  1833. /*
  1834. * Fetch our task data from the task
  1835. */
  1836. td = (task_data *)slapi_task_get_data(task);
  1837. slapi_task_begin(task, 1);
  1838. slapi_task_log_notice(task, "Automember rebuild task starting (base dn: (%s) filter (%s)...\n",
  1839. slapi_sdn_get_dn(td->base_dn),td->filter_str);
  1840. slapi_task_log_status(task, "Automember rebuild task starting (base dn: (%s) filter (%s)...\n",
  1841. slapi_sdn_get_dn(td->base_dn),td->filter_str);
  1842. /*
  1843. * Set the bind dn in the local thread data
  1844. */
  1845. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  1846. /*
  1847. * Search the database
  1848. */
  1849. search_pb = slapi_pblock_new();
  1850. slapi_search_internal_set_pb_ext(search_pb, td->base_dn, td->scope, td->filter_str, NULL,
  1851. 0, NULL, NULL, automember_get_plugin_id(), 0);
  1852. slapi_search_internal_pb(search_pb);
  1853. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1854. if (LDAP_SUCCESS != result){
  1855. slapi_task_log_notice(task, "Automember rebuild membership task unable to search"
  1856. " on base (%s) filter (%s) error (%d)\n", slapi_sdn_get_dn(td->base_dn),
  1857. td->filter_str, result);
  1858. slapi_task_log_status(task, "Automember rebuild membership task unable to search"
  1859. " on base (%s) filter (%s) error (%d)\n", slapi_sdn_get_dn(td->base_dn),
  1860. td->filter_str, result);
  1861. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1862. "automember_rebuild_task_thread - Unable to search on base (%s) filter (%s) error (%d)\n",
  1863. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  1864. goto out;
  1865. }
  1866. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1867. /*
  1868. * If this is a backend txn plugin, start the transaction
  1869. */
  1870. if (plugin_is_betxn) {
  1871. Slapi_Backend *be = slapi_be_select(td->base_dn);
  1872. if (be) {
  1873. fixup_pb = slapi_pblock_new();
  1874. slapi_pblock_set(fixup_pb, SLAPI_BACKEND, be);
  1875. if(slapi_back_transaction_begin(fixup_pb) != LDAP_SUCCESS){
  1876. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1877. "automember_rebuild_task_thread - Failed to start transaction\n");
  1878. }
  1879. } else {
  1880. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1881. "automember_rebuild_task_thread - Failed to get be backend from %s\n",
  1882. slapi_sdn_get_dn(td->base_dn));
  1883. }
  1884. }
  1885. /*
  1886. * Grab the config read lock, and loop over the entries
  1887. */
  1888. automember_config_read_lock();
  1889. for (i = 0; entries && (entries[i] != NULL); i++){
  1890. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  1891. list = PR_LIST_HEAD(g_automember_config);
  1892. while (list != g_automember_config) {
  1893. config = (struct configEntry *)list;
  1894. /* Does the entry meet scope and filter requirements? */
  1895. if (slapi_dn_issuffix(slapi_entry_get_dn(entries[i]), config->scope) &&
  1896. (slapi_filter_test_simple(entries[i], config->filter) == 0))
  1897. {
  1898. if (slapi_is_shutting_down() ||
  1899. automember_update_membership(config, entries[i], NULL)) {
  1900. result = SLAPI_PLUGIN_FAILURE;
  1901. automember_config_unlock();
  1902. goto out;
  1903. }
  1904. }
  1905. list = PR_NEXT_LINK(list);
  1906. }
  1907. }
  1908. }
  1909. automember_config_unlock();
  1910. out:
  1911. if (plugin_is_betxn && fixup_pb) {
  1912. if (i == 0 || result != 0) { /* no updates performed */
  1913. slapi_back_transaction_abort(fixup_pb);
  1914. } else {
  1915. slapi_back_transaction_commit(fixup_pb);
  1916. }
  1917. slapi_pblock_destroy(fixup_pb);
  1918. }
  1919. slapi_free_search_results_internal(search_pb);
  1920. slapi_pblock_destroy(search_pb);
  1921. if(result){
  1922. /* error */
  1923. slapi_task_log_notice(task, "Automember rebuild task aborted. Error (%d)", result);
  1924. slapi_task_log_status(task, "Automember rebuild task aborted. Error (%d)", result);
  1925. } else {
  1926. slapi_task_log_notice(task, "Automember rebuild task finished. Processed (%d) entries.", i);
  1927. slapi_task_log_status(task, "Automember rebuild task finished. Processed (%d) entries.", i);
  1928. }
  1929. slapi_task_inc_progress(task);
  1930. slapi_task_finish(task, result);
  1931. slapi_task_dec_refcount(task);
  1932. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1933. "automember_rebuild_task_thread - Refcount decremented.\n" );
  1934. }
  1935. /*
  1936. * Export an ldif of the changes that would be made if we ran the automember rebuild membership task
  1937. *
  1938. * task entry:
  1939. *
  1940. * dn: cn=my export task, cn=automember export updates,cn=tasks,cn=config
  1941. * objectClass: top
  1942. * objectClass: extensibleObject
  1943. * cn: my export task
  1944. * basedn: dc=example,dc=com
  1945. * filter: (uid=*)
  1946. * scope: sub
  1947. * ldif: /tmp/automem-updates.ldif
  1948. */
  1949. static int
  1950. automember_task_add_export_updates(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1951. int *returncode, char *returntext, void *arg)
  1952. {
  1953. int rv = SLAPI_DSE_CALLBACK_OK;
  1954. task_data *mytaskdata = NULL;
  1955. Slapi_Task *task = NULL;
  1956. PRThread *thread = NULL;
  1957. char *bind_dn = NULL;
  1958. const char *base_dn = NULL;
  1959. const char *filter = NULL;
  1960. const char *ldif = NULL;
  1961. const char *scope = NULL;
  1962. *returncode = LDAP_SUCCESS;
  1963. if((ldif = fetch_attr(e, "ldif", 0)) == NULL){
  1964. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1965. rv = SLAPI_DSE_CALLBACK_ERROR;
  1966. goto out;
  1967. }
  1968. if((base_dn = fetch_attr(e, "basedn", 0)) == NULL){
  1969. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1970. rv = SLAPI_DSE_CALLBACK_ERROR;
  1971. goto out;
  1972. }
  1973. if((filter = fetch_attr(e, "filter", 0)) == NULL){
  1974. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1975. rv = SLAPI_DSE_CALLBACK_ERROR;
  1976. goto out;
  1977. }
  1978. scope = fetch_attr(e, "scope", "sub");
  1979. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  1980. mytaskdata = (task_data*)slapi_ch_calloc(1, sizeof(task_data));
  1981. if (mytaskdata == NULL){
  1982. *returncode = LDAP_OPERATIONS_ERROR;
  1983. rv = SLAPI_DSE_CALLBACK_ERROR;
  1984. goto out;
  1985. }
  1986. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  1987. mytaskdata->ldif_out = slapi_ch_strdup(ldif);
  1988. mytaskdata->base_dn = slapi_sdn_new_dn_byval(base_dn);
  1989. mytaskdata->filter_str = slapi_ch_strdup(filter);
  1990. if(scope){
  1991. if(strcasecmp(scope,"sub")== 0){
  1992. mytaskdata->scope = 2;
  1993. } else if(strcasecmp(scope,"one")== 0){
  1994. mytaskdata->scope = 1;
  1995. } else if(strcasecmp(scope,"base")== 0){
  1996. mytaskdata->scope = 0;
  1997. } else {
  1998. /* Hmm, possible typo, use subtree */
  1999. mytaskdata->scope = 2;
  2000. }
  2001. } else {
  2002. /* subtree by default */
  2003. mytaskdata->scope = 2;
  2004. }
  2005. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  2006. slapi_task_set_destructor_fn(task, automember_task_export_destructor);
  2007. slapi_task_set_data(task, mytaskdata);
  2008. /*
  2009. * Start the task as a separate thread
  2010. */
  2011. thread = PR_CreateThread(PR_USER_THREAD, automember_export_task_thread,
  2012. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2013. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2014. if (thread == NULL){
  2015. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2016. "automember_task_add_export_updates - Unable to create export task thread!\n");
  2017. *returncode = LDAP_OPERATIONS_ERROR;
  2018. rv = SLAPI_DSE_CALLBACK_ERROR;
  2019. slapi_task_finish(task, *returncode);
  2020. } else {
  2021. rv = SLAPI_DSE_CALLBACK_OK;
  2022. }
  2023. out:
  2024. return rv;
  2025. }
  2026. void automember_export_task_thread(void *arg)
  2027. {
  2028. Slapi_Task *task = (Slapi_Task *)arg;
  2029. Slapi_PBlock *search_pb = NULL;
  2030. Slapi_Entry **entries = NULL;
  2031. int result = SLAPI_DSE_CALLBACK_OK;
  2032. struct configEntry *config = NULL;
  2033. PRCList *list = NULL;
  2034. task_data *td = NULL;
  2035. PRFileDesc *ldif_fd;
  2036. int i = 0;
  2037. int rc = 0;
  2038. if (!task) {
  2039. return; /* no task */
  2040. }
  2041. slapi_task_inc_refcount(task);
  2042. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2043. "automember_export_task_thread - Refcount incremented.\n" );
  2044. td = (task_data *)slapi_task_get_data(task);
  2045. slapi_task_begin(task, 1);
  2046. slapi_task_log_notice(task, "Automember export task starting. Exporting changes to (%s)", td->ldif_out);
  2047. slapi_task_log_status(task, "Automember export task starting. Exporting changes to (%s)", td->ldif_out);
  2048. /* make sure we can open the ldif file */
  2049. if (( ldif_fd = PR_Open( td->ldif_out, PR_CREATE_FILE | PR_WRONLY, DEFAULT_FILE_MODE )) == NULL ){
  2050. rc = PR_GetOSError();
  2051. slapi_task_log_notice(task, "Automember export task could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2052. td->ldif_out, rc, slapi_system_strerror(rc));
  2053. slapi_task_log_status(task, "Automember export task could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2054. td->ldif_out, rc, slapi_system_strerror(rc) );
  2055. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2056. "automember_export_task_thread - Could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2057. td->ldif_out, rc, slapi_system_strerror(rc) );
  2058. result = SLAPI_DSE_CALLBACK_ERROR;
  2059. goto out;
  2060. }
  2061. /*
  2062. * Set the bind dn in the local thread data
  2063. */
  2064. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  2065. /*
  2066. * Search the database
  2067. */
  2068. search_pb = slapi_pblock_new();
  2069. slapi_search_internal_set_pb_ext(search_pb, td->base_dn, td->scope, td->filter_str, NULL,
  2070. 0, NULL, NULL, automember_get_plugin_id(), 0);
  2071. slapi_search_internal_pb(search_pb);
  2072. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2073. if (LDAP_SUCCESS != result){
  2074. slapi_task_log_notice(task, "Automember task failed to search on base (%s) filter (%s) error (%d)\n",
  2075. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2076. slapi_task_log_status(task, "Automember task failed to search on base (%s) filter (%s) error (%d)\n",
  2077. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2078. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2079. "automember_export_task_thread - Unable to search on base (%s) filter (%s) error (%d)\n",
  2080. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2081. goto out;
  2082. }
  2083. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2084. /*
  2085. * Grab the config read lock, and loop over the entries
  2086. */
  2087. automember_config_read_lock();
  2088. for (i = 0; entries && (entries[i] != NULL); i++){
  2089. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2090. list = PR_LIST_HEAD(g_automember_config);
  2091. while (list != g_automember_config) {
  2092. config = (struct configEntry *)list;
  2093. if (slapi_dn_issuffix(slapi_sdn_get_dn(td->base_dn), config->scope) &&
  2094. (slapi_filter_test_simple(entries[i], config->filter) == 0))
  2095. {
  2096. if (slapi_is_shutting_down() ||
  2097. automember_update_membership(config, entries[i], ldif_fd)) {
  2098. result = SLAPI_DSE_CALLBACK_ERROR;
  2099. automember_config_unlock();
  2100. goto out;
  2101. }
  2102. }
  2103. list = PR_NEXT_LINK(list);
  2104. }
  2105. }
  2106. }
  2107. automember_config_unlock();
  2108. out:
  2109. slapi_free_search_results_internal(search_pb);
  2110. slapi_pblock_destroy(search_pb);
  2111. if(ldif_fd){
  2112. PR_Close(ldif_fd);
  2113. }
  2114. if(result){
  2115. /* error */
  2116. slapi_task_log_notice(task, "Automember export task aborted. Error (%d)", result);
  2117. slapi_task_log_status(task, "Automember export task aborted. Error (%d)", result);
  2118. } else {
  2119. slapi_task_log_notice(task, "Automember export task finished. Processed (%d) entries.", i);
  2120. slapi_task_log_status(task, "Automember export task finished. Processed (%d) entries.", i);
  2121. }
  2122. slapi_task_inc_progress(task);
  2123. slapi_task_finish(task, result);
  2124. slapi_task_dec_refcount(task);
  2125. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2126. "automember_export_task_thread - Refcount decremented.\n" );
  2127. }
  2128. /*
  2129. * Export an ldif of the changes that would be made from the entries
  2130. * in the provided ldif file
  2131. *
  2132. * task entry:
  2133. *
  2134. * dn: cn=my map task, cn=automember map updates,cn=tasks,cn=config
  2135. * objectClass: top
  2136. * objectClass: extensibleObject
  2137. * cn: my export task
  2138. * ldif_in: /tmp/entries.ldif
  2139. * ldif_out: /tmp/automem-updates.ldif
  2140. */
  2141. static int
  2142. automember_task_add_map_entries(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2143. int *returncode, char *returntext, void *arg)
  2144. {
  2145. int rv = SLAPI_DSE_CALLBACK_OK;
  2146. task_data *mytaskdata = NULL;
  2147. Slapi_Task *task = NULL;
  2148. PRThread *thread = NULL;
  2149. char *bind_dn;
  2150. const char *ldif_out;
  2151. const char *ldif_in;
  2152. *returncode = LDAP_SUCCESS;
  2153. /*
  2154. * Get the params
  2155. */
  2156. if((ldif_in = fetch_attr(e, "ldif_in", 0)) == NULL){
  2157. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2158. rv = SLAPI_DSE_CALLBACK_ERROR;
  2159. goto out;
  2160. }
  2161. if((ldif_out = fetch_attr(e, "ldif_out", 0)) == NULL){
  2162. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2163. rv = SLAPI_DSE_CALLBACK_ERROR;
  2164. goto out;
  2165. }
  2166. /*
  2167. * Setup the task data
  2168. */
  2169. mytaskdata = (task_data*)slapi_ch_calloc(1, sizeof(task_data));
  2170. if (mytaskdata == NULL){
  2171. *returncode = LDAP_OPERATIONS_ERROR;
  2172. rv = SLAPI_DSE_CALLBACK_ERROR;
  2173. goto out;
  2174. }
  2175. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  2176. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  2177. mytaskdata->ldif_out = slapi_ch_strdup(ldif_out);
  2178. mytaskdata->ldif_in = slapi_ch_strdup(ldif_in);
  2179. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  2180. slapi_task_set_destructor_fn(task, automember_task_map_destructor);
  2181. slapi_task_set_data(task, mytaskdata);
  2182. /*
  2183. * Start the task as a separate thread
  2184. */
  2185. thread = PR_CreateThread(PR_USER_THREAD, automember_map_task_thread,
  2186. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2187. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2188. if (thread == NULL){
  2189. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2190. "automember_task_add_map_entries - Unable to create map task thread!\n");
  2191. *returncode = LDAP_OPERATIONS_ERROR;
  2192. rv = SLAPI_DSE_CALLBACK_ERROR;
  2193. slapi_task_finish(task, *returncode);
  2194. } else {
  2195. rv = SLAPI_DSE_CALLBACK_OK;
  2196. }
  2197. out:
  2198. return rv;
  2199. }
  2200. /*
  2201. * Read in the text entries from ldif_in, and convert them to slapi_entries.
  2202. * Then, write to ldif_out what the updates would be if these entries were added
  2203. */
  2204. void automember_map_task_thread(void *arg)
  2205. {
  2206. Slapi_Task *task = (Slapi_Task *)arg;
  2207. Slapi_Entry *e = NULL;
  2208. int result = SLAPI_DSE_CALLBACK_OK;
  2209. struct configEntry *config = NULL;
  2210. PRCList *list = NULL;
  2211. task_data *td = NULL;
  2212. PRFileDesc *ldif_fd_out = NULL;
  2213. char *entrystr = NULL;
  2214. char *errstr = NULL;
  2215. #if defined(USE_OPENLDAP)
  2216. int buflen = 0;
  2217. LDIFFP *ldif_fd_in = NULL;
  2218. ldif_record_lineno_t lineno = 0;
  2219. #else
  2220. FILE *ldif_fd_in = NULL;
  2221. int lineno = 0;
  2222. #endif
  2223. int rc = 0;
  2224. if (!task) {
  2225. return; /* no task */
  2226. }
  2227. slapi_task_inc_refcount(task);
  2228. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2229. "automember_map_task_thread - Refcount incremented.\n" );
  2230. td = (task_data *)slapi_task_get_data(task);
  2231. slapi_task_begin(task, 1);
  2232. slapi_task_log_notice(task, "Automember map task starting... Reading entries from (%s)"
  2233. " and writing the updates to (%s)",td->ldif_in, td->ldif_out);
  2234. slapi_task_log_status(task, "Automember map task starting... Reading entries from (%s)"
  2235. " and writing the updates to (%s)",td->ldif_in, td->ldif_out);
  2236. /* make sure we can open the ldif files */
  2237. if(( ldif_fd_out = PR_Open( td->ldif_out, PR_CREATE_FILE | PR_WRONLY, DEFAULT_FILE_MODE )) == NULL ){
  2238. rc = PR_GetOSError();
  2239. slapi_task_log_notice(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2240. td->ldif_out, rc, slapi_system_strerror(rc));
  2241. slapi_task_log_status(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2242. td->ldif_out, rc, slapi_system_strerror(rc));
  2243. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2244. "automember_map_task_thread - Could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2245. td->ldif_out, rc, slapi_system_strerror(rc) );
  2246. result = SLAPI_DSE_CALLBACK_ERROR;
  2247. goto out;
  2248. }
  2249. #if defined(USE_OPENLDAP)
  2250. if(( ldif_fd_in = ldif_open(td->ldif_in, "r")) == NULL ){
  2251. rc = errno;
  2252. errstr = strerror(rc);
  2253. #else
  2254. if(( ldif_fd_in = fopen( td->ldif_in, "r")) == NULL ){
  2255. rc = PR_GetOSError();
  2256. errstr = (char *)slapi_system_strerror(rc);
  2257. #endif
  2258. slapi_task_log_notice(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2259. td->ldif_in, rc, errstr);
  2260. slapi_task_log_status(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2261. td->ldif_in, rc, errstr);
  2262. slapi_log_err(SLAPI_LOG_ERR, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2263. "automember_map_task_thread - Could not open ldif file \"%s\" for reading, error %d (%s)\n",
  2264. td->ldif_in, rc, errstr );
  2265. result = SLAPI_DSE_CALLBACK_ERROR;
  2266. goto out;
  2267. }
  2268. /*
  2269. * Convert each LDIF entry to a slapi_entry
  2270. */
  2271. automember_config_read_lock();
  2272. #if defined(USE_OPENLDAP)
  2273. while (ldif_read_record(ldif_fd_in, &lineno, &entrystr, &buflen)){
  2274. buflen = 0;
  2275. #else
  2276. while ((entrystr = ldif_get_entry(ldif_fd_in, &lineno)) != NULL){
  2277. #endif
  2278. e = slapi_str2entry( entrystr, 0 );
  2279. if ( e != NULL ){
  2280. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2281. list = PR_LIST_HEAD(g_automember_config);
  2282. while (list != g_automember_config) {
  2283. config = (struct configEntry *)list;
  2284. if (slapi_dn_issuffix(slapi_entry_get_dn_const(e), config->scope) &&
  2285. (slapi_filter_test_simple(e, config->filter) == 0))
  2286. {
  2287. if (slapi_is_shutting_down() ||
  2288. automember_update_membership(config, e, ldif_fd_out)) {
  2289. result = SLAPI_DSE_CALLBACK_ERROR;
  2290. slapi_entry_free(e);
  2291. slapi_ch_free_string(&entrystr);
  2292. automember_config_unlock();
  2293. goto out;
  2294. }
  2295. }
  2296. list = PR_NEXT_LINK(list);
  2297. }
  2298. }
  2299. slapi_entry_free(e);
  2300. } else {
  2301. /* invalid entry */
  2302. slapi_task_log_notice(task, "Automember map task, skipping invalid entry.");
  2303. slapi_task_log_status(task, "Automember map task, skipping invalid entry.");
  2304. }
  2305. slapi_ch_free_string(&entrystr);
  2306. }
  2307. automember_config_unlock();
  2308. out:
  2309. if(ldif_fd_out){
  2310. PR_Close(ldif_fd_out);
  2311. }
  2312. if(ldif_fd_in){
  2313. #if defined(USE_OPENLDAP)
  2314. ldif_close(ldif_fd_in);
  2315. #else
  2316. fclose(ldif_fd_in);
  2317. #endif
  2318. }
  2319. slapi_task_inc_progress(task);
  2320. slapi_task_finish(task, result);
  2321. slapi_task_dec_refcount(task);
  2322. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2323. "automember_map_task_thread - Refcount decremented.\n" );
  2324. }
  2325. /*
  2326. * automember_modrdn_post_op()
  2327. *
  2328. * Reloads config if config entries move
  2329. * into or out of our config scope.
  2330. */
  2331. static int
  2332. automember_modrdn_post_op(Slapi_PBlock *pb)
  2333. {
  2334. Slapi_Entry *post_e = NULL;
  2335. Slapi_DN *old_sdn = NULL;
  2336. Slapi_DN *new_sdn = NULL;
  2337. struct configEntry *config = NULL;
  2338. PRCList *list = NULL;
  2339. int rc = SLAPI_PLUGIN_SUCCESS;
  2340. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2341. "--> automember_modrdn_post_op\n");
  2342. /* Just bail if we aren't ready to service requests yet. */
  2343. if (!automember_oktodo(pb)){
  2344. return SLAPI_PLUGIN_SUCCESS;
  2345. }
  2346. /*
  2347. * Reload config if an existing config entry was renamed,
  2348. * or if the new dn brings an entry into the scope of the
  2349. * config entries.
  2350. */
  2351. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  2352. if (post_e) {
  2353. new_sdn = slapi_entry_get_sdn(post_e);
  2354. } else {
  2355. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2356. "automember_modrdn_post_op - Error "
  2357. "retrieving post-op entry\n");
  2358. return SLAPI_PLUGIN_FAILURE;
  2359. }
  2360. if ((old_sdn = automember_get_sdn(pb))) {
  2361. if (automember_dn_is_config(old_sdn) || automember_dn_is_config(new_sdn)){
  2362. automember_load_config();
  2363. }
  2364. } else {
  2365. slapi_log_err(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2366. "automember_modrdn_post_op - Error "
  2367. "retrieving dn\n");
  2368. return SLAPI_PLUGIN_FAILURE;
  2369. }
  2370. /* If replication, just bail. */
  2371. if (automember_isrepl(pb)) {
  2372. return SLAPI_PLUGIN_SUCCESS;
  2373. }
  2374. /*
  2375. * Check if a config entry applies to the entry(post modrdn)
  2376. */
  2377. automember_config_read_lock();
  2378. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2379. list = PR_LIST_HEAD(g_automember_config);
  2380. while (list != g_automember_config) {
  2381. config = (struct configEntry *)list;
  2382. /* Does the entry meet scope and filter requirements? */
  2383. if (slapi_dn_issuffix(slapi_sdn_get_dn(new_sdn), config->scope) &&
  2384. (slapi_filter_test_simple(post_e, config->filter) == 0)) {
  2385. /* Find out what membership changes are needed and make them. */
  2386. if(automember_update_membership(config, post_e, NULL)){
  2387. rc = SLAPI_PLUGIN_FAILURE;
  2388. break;
  2389. }
  2390. }
  2391. list = PR_NEXT_LINK(list);
  2392. }
  2393. }
  2394. automember_config_unlock();
  2395. if(rc){
  2396. char errtxt[SLAPI_DSE_RETURNTEXT_SIZE];
  2397. int result = LDAP_UNWILLING_TO_PERFORM;
  2398. PR_snprintf(errtxt, SLAPI_DSE_RETURNTEXT_SIZE, "Automember Plugin update unexpectedly failed. "
  2399. "Please see the server errors log for more information.\n");
  2400. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &result);
  2401. slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, &errtxt);
  2402. }
  2403. slapi_log_err(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2404. "<-- automember_modrdn_post_op (%d)\n", rc);
  2405. return rc;
  2406. }