1
0

automember.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  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();
  49. static void automember_delete_config();
  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();
  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()
  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_error(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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  200. "automember_init: failed to register postop plugin\n");
  201. status = -1;
  202. }
  203. }
  204. slapi_log_error(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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(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_error(SLAPI_LOG_FATAL, 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_error(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_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  302. "automember_start: unable to load plug-in configuration\n");
  303. return -1;
  304. }
  305. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  306. "auto membership plug-in: ready for service\n");
  307. slapi_log_error(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_error(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_error(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()
  354. {
  355. int status = 0;
  356. int result;
  357. int i;
  358. Slapi_PBlock *search_pb;
  359. Slapi_Entry **entries = NULL;
  360. slapi_log_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(SLAPI_LOG_FATAL, 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_error(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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  555. "automember_parse_config_entry: Invalid "
  556. "%s config setting for config entry \"%s\" "
  557. "(value: \"%s\").\n", AUTOMEMBER_GROUPING_ATTR_TYPE,
  558. entry->dn, value);
  559. ret = -1;
  560. }
  561. slapi_ch_free_string(&value);
  562. if (ret != 0) {
  563. goto bail;
  564. }
  565. } else {
  566. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  567. "automember_parse_config_entry: The %s config "
  568. "setting is required for config entry \"%s\".\n",
  569. AUTOMEMBER_GROUPING_ATTR_TYPE, entry->dn);
  570. ret = -1;
  571. goto bail;
  572. }
  573. /* Find all child regex rule entries */
  574. search_pb = slapi_pblock_new();
  575. slapi_search_internal_set_pb(search_pb, entry->dn, LDAP_SCOPE_SUBTREE,
  576. AUTOMEMBER_REGEX_RULE_FILTER, NULL, 0, NULL,
  577. NULL, automember_get_plugin_id(), 0);
  578. slapi_search_internal_pb(search_pb);
  579. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  580. /* If this is a new config entry being added, it won't exist yet
  581. * when we are simply validating config. We can just ignore no
  582. * such object errors. */
  583. if ((LDAP_SUCCESS != result) && (LDAP_NO_SUCH_OBJECT != result)) {
  584. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  585. "automember_parse_config_entry: Error searching "
  586. "for child rule entries for config \"%s\" (err=%d).",
  587. entry->dn, result);
  588. ret = -1;
  589. goto bail;
  590. }
  591. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  592. &rule_entries);
  593. /* Go through each child rule entry and parse it. */
  594. for (i = 0; rule_entries && (rule_entries[i] != NULL); i++) {
  595. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  596. "automember_parse_config_entry: parsing regex rule entry "
  597. "\"%s\".\n", slapi_entry_get_dn(rule_entries[i]));
  598. automember_parse_regex_entry(entry, rule_entries[i]);
  599. }
  600. /* If we were only called to validate config, we can
  601. * just bail out before applying the config changes */
  602. if (apply == 0) {
  603. goto bail;
  604. }
  605. /* Add the config object to the list. We order by scope. */
  606. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  607. list = PR_LIST_HEAD(g_automember_config);
  608. while (list != g_automember_config) {
  609. config_entry = (struct configEntry *) list;
  610. /* If the config entry we are adding has a scope that is
  611. * a child of the scope of the current list item, we insert
  612. * the entry before that list item. */
  613. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  614. PR_INSERT_BEFORE(&(entry->list), list);
  615. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  616. "store [%s] before [%s] \n", entry->dn,
  617. config_entry->dn);
  618. entry_added = 1;
  619. break;
  620. }
  621. list = PR_NEXT_LINK(list);
  622. /* If we hit the end of the list, add to the tail. */
  623. if (g_automember_config == list) {
  624. PR_INSERT_BEFORE(&(entry->list), list);
  625. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  626. "store [%s] at tail\n", entry->dn);
  627. entry_added = 1;
  628. break;
  629. }
  630. }
  631. } else {
  632. /* first entry */
  633. PR_INSERT_LINK(&(entry->list), g_automember_config);
  634. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  635. "store [%s] at head \n", entry->dn);
  636. entry_added = 1;
  637. }
  638. bail:
  639. if (0 == entry_added) {
  640. /* Don't log error if we weren't asked to apply config */
  641. if ((apply != 0) && (entry != NULL)) {
  642. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  643. "automember_parse_config_entry: Invalid config entry "
  644. "[%s] skipped\n", entry->dn);
  645. }
  646. automember_free_config_entry(&entry);
  647. } else {
  648. ret = 0;
  649. }
  650. slapi_ch_free_string(&filter_str);
  651. slapi_filter_free(filter, 1);
  652. slapi_free_search_results_internal(search_pb);
  653. slapi_pblock_destroy(search_pb);
  654. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  655. "<-- automember_parse_config_entry\n");
  656. return ret;
  657. }
  658. static void
  659. automember_free_config_entry(struct configEntry ** entry)
  660. {
  661. struct configEntry *e = *entry;
  662. if (e == NULL)
  663. return;
  664. if (e->dn) {
  665. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  666. "freeing config entry [%s]\n", e->dn);
  667. slapi_ch_free_string(&e->dn);
  668. }
  669. if (e->scope) {
  670. slapi_ch_free_string(&e->scope);
  671. }
  672. if (e->filter) {
  673. slapi_filter_free(e->filter, 1);
  674. }
  675. if (e->exclusive_rules) {
  676. PRCList *list;
  677. /* Clear out the list contents. */
  678. while (!PR_CLIST_IS_EMPTY((PRCList *)e->exclusive_rules)) {
  679. list = PR_LIST_HEAD((PRCList *)e->exclusive_rules);
  680. PR_REMOVE_LINK(list);
  681. automember_free_regex_rule((struct automemberRegexRule *)list);
  682. }
  683. /* Free the list itself. */
  684. slapi_ch_free((void **)&(e->exclusive_rules));
  685. }
  686. /* Clear out the list contents. */
  687. if (e->inclusive_rules) {
  688. PRCList *list;
  689. while (!PR_CLIST_IS_EMPTY((PRCList *)e->inclusive_rules)) {
  690. list = PR_LIST_HEAD((PRCList *)e->inclusive_rules);
  691. PR_REMOVE_LINK(list);
  692. automember_free_regex_rule((struct automemberRegexRule *)list);
  693. }
  694. /* Free the list itself. */
  695. slapi_ch_free((void **)&(e->inclusive_rules));
  696. }
  697. if (e->default_groups) {
  698. slapi_ch_array_free(e->default_groups);
  699. }
  700. if (e->grouping_attr) {
  701. slapi_ch_free_string(&e->grouping_attr);
  702. }
  703. if (e->grouping_value) {
  704. slapi_ch_free_string(&e->grouping_value);
  705. }
  706. slapi_ch_free((void **)entry);
  707. }
  708. static void
  709. automember_delete_configEntry(PRCList *entry)
  710. {
  711. PR_REMOVE_LINK(entry);
  712. automember_free_config_entry((struct configEntry **) &entry);
  713. }
  714. static void
  715. automember_delete_config()
  716. {
  717. PRCList *list;
  718. /* Delete the config cache. */
  719. while (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  720. list = PR_LIST_HEAD(g_automember_config);
  721. automember_delete_configEntry(list);
  722. }
  723. 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_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  731. "--> automember_get_sdn\n");
  732. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  733. slapi_log_error(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()
  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_error(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_error(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_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  793. "--> automember_oktodo\n" );
  794. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0) {
  795. slapi_log_error( SLAPI_LOG_FATAL, 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_error( 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_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  818. "--> automember_isrepl\n" );
  819. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  820. slapi_log_error( 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_error(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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error( SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error( SLAPI_LOG_FATAL, 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_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1099. "automember_parse_grouping_attr: Value for grouping attribute "
  1100. "is not in the correct format. The grouping 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_error( SLAPI_LOG_FATAL, 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_error( SLAPI_LOG_FATAL, 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_error( SLAPI_LOG_FATAL, 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_error( SLAPI_LOG_FATAL, 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_error(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_error(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_error(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_error(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_error(SLAPI_LOG_FATAL, 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_error(SLAPI_LOG_FATAL, 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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(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_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1632. "automember_del_post_op: Error "
  1633. "retrieving dn\n");
  1634. }
  1635. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1636. "<-- automember_del_post_op\n");
  1637. return SLAPI_PLUGIN_SUCCESS;
  1638. }
  1639. typedef struct _task_data
  1640. {
  1641. char *filter_str;
  1642. char *ldif_out;
  1643. char *ldif_in;
  1644. Slapi_DN *base_dn;
  1645. char *bind_dn;
  1646. int scope;
  1647. } task_data;
  1648. /*
  1649. * extract a single value from the entry (as a string) -- if it's not in the
  1650. * entry, the default will be returned (which can be NULL).
  1651. * you do not need to free anything returned by this.
  1652. */
  1653. const char *
  1654. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  1655. {
  1656. Slapi_Value *val = NULL;
  1657. Slapi_Attr *attr;
  1658. if(slapi_entry_attr_find(e, attrname, &attr) != 0){
  1659. return default_val;
  1660. }
  1661. slapi_attr_first_value(attr, &val);
  1662. return slapi_value_get_string(val);
  1663. }
  1664. static void
  1665. automember_task_destructor(Slapi_Task *task)
  1666. {
  1667. if (task) {
  1668. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1669. while (slapi_task_get_refcount(task) > 0) {
  1670. /* Yield to wait for the fixup task finishes. */
  1671. DS_Sleep (PR_MillisecondsToInterval(100));
  1672. }
  1673. if (mydata) {
  1674. slapi_ch_free_string(&mydata->bind_dn);
  1675. slapi_sdn_free(&mydata->base_dn);
  1676. slapi_ch_free_string(&mydata->filter_str);
  1677. slapi_ch_free((void **)&mydata);
  1678. }
  1679. }
  1680. }
  1681. static void
  1682. automember_task_export_destructor(Slapi_Task *task)
  1683. {
  1684. if (task) {
  1685. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1686. while (slapi_task_get_refcount(task) > 0) {
  1687. /* Yield to wait for the fixup task finishes. */
  1688. DS_Sleep (PR_MillisecondsToInterval(100));
  1689. }
  1690. if (mydata) {
  1691. slapi_ch_free_string(&mydata->ldif_out);
  1692. slapi_ch_free_string(&mydata->bind_dn);
  1693. slapi_sdn_free(&mydata->base_dn);
  1694. slapi_ch_free_string(&mydata->filter_str);
  1695. slapi_ch_free((void **)&mydata);
  1696. }
  1697. }
  1698. }
  1699. static void
  1700. automember_task_map_destructor(Slapi_Task *task)
  1701. {
  1702. if (task) {
  1703. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1704. while (slapi_task_get_refcount(task) > 0) {
  1705. /* Yield to wait for the fixup task finishes. */
  1706. DS_Sleep (PR_MillisecondsToInterval(100));
  1707. }
  1708. if (mydata) {
  1709. slapi_ch_free_string(&mydata->ldif_out);
  1710. slapi_ch_free_string(&mydata->ldif_in);
  1711. slapi_ch_free_string(&mydata->bind_dn);
  1712. slapi_ch_free((void **)&mydata);
  1713. }
  1714. }
  1715. }
  1716. /*
  1717. * automember_task_add
  1718. *
  1719. * This task is designed to "retro-fit" entries that existed prior to
  1720. * enabling this plugin. This can be an expensive task to run, but it's
  1721. * better than processing every modify operation in an attempt to catch
  1722. * entries that have not been processed.
  1723. *
  1724. * task entry:
  1725. *
  1726. * dn: cn=my rebuild task, cn=automember rebuild membership,cn=tasks,cn=config
  1727. * objectClass: top
  1728. * objectClass: extensibleObject
  1729. * cn: my rebuild task
  1730. * basedn: dc=example,dc=com
  1731. * filter: (uid=*)
  1732. * scope: sub
  1733. *
  1734. * basedn and filter are required. If scope is omitted, the default is sub
  1735. */
  1736. static int
  1737. automember_task_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1738. int *returncode, char *returntext, void *arg)
  1739. {
  1740. int rv = SLAPI_DSE_CALLBACK_OK;
  1741. task_data *mytaskdata = NULL;
  1742. Slapi_Task *task = NULL;
  1743. PRThread *thread = NULL;
  1744. char *bind_dn = NULL;
  1745. const char *base_dn;
  1746. const char *filter;
  1747. const char *scope;
  1748. *returncode = LDAP_SUCCESS;
  1749. /*
  1750. * Grab the task params
  1751. */
  1752. if((base_dn = fetch_attr(e, "basedn", 0)) == NULL){
  1753. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1754. rv = SLAPI_DSE_CALLBACK_ERROR;
  1755. goto out;
  1756. }
  1757. if((filter = fetch_attr(e, "filter", 0)) == NULL){
  1758. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1759. rv = SLAPI_DSE_CALLBACK_ERROR;
  1760. goto out;
  1761. }
  1762. scope = fetch_attr(e, "scope", "sub");
  1763. /*
  1764. * setup our task data
  1765. */
  1766. mytaskdata = (task_data*)slapi_ch_calloc(1, sizeof(task_data));
  1767. if (mytaskdata == NULL){
  1768. *returncode = LDAP_OPERATIONS_ERROR;
  1769. rv = SLAPI_DSE_CALLBACK_ERROR;
  1770. goto out;
  1771. }
  1772. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  1773. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  1774. mytaskdata->base_dn = slapi_sdn_new_dn_byval(base_dn);
  1775. mytaskdata->filter_str = slapi_ch_strdup(filter);
  1776. if(scope){
  1777. if(strcasecmp(scope,"sub")== 0){
  1778. mytaskdata->scope = 2;
  1779. } else if(strcasecmp(scope,"one")== 0){
  1780. mytaskdata->scope = 1;
  1781. } else if(strcasecmp(scope,"base")== 0){
  1782. mytaskdata->scope = 0;
  1783. } else {
  1784. /* Hmm, possible typo, use subtree */
  1785. mytaskdata->scope = 2;
  1786. }
  1787. } else {
  1788. /* subtree by default */
  1789. mytaskdata->scope = 2;
  1790. }
  1791. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  1792. slapi_task_set_destructor_fn(task, automember_task_destructor);
  1793. slapi_task_set_data(task, mytaskdata);
  1794. /*
  1795. * Start the task as a separate thread
  1796. */
  1797. thread = PR_CreateThread(PR_USER_THREAD, automember_rebuild_task_thread,
  1798. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1799. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1800. if (thread == NULL){
  1801. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1802. "unable to create task thread!\n");
  1803. *returncode = LDAP_OPERATIONS_ERROR;
  1804. slapi_task_finish(task, *returncode);
  1805. rv = SLAPI_DSE_CALLBACK_ERROR;
  1806. } else {
  1807. rv = SLAPI_DSE_CALLBACK_OK;
  1808. }
  1809. out:
  1810. return rv;
  1811. }
  1812. /*
  1813. * automember_rebuild_task_thread()
  1814. *
  1815. * Search using the basedn, filter, and scope provided from the task data.
  1816. * Then loop of each entry, and apply the membership if applicable.
  1817. */
  1818. void automember_rebuild_task_thread(void *arg)
  1819. {
  1820. Slapi_Task *task = (Slapi_Task *)arg;
  1821. struct configEntry *config = NULL;
  1822. Slapi_PBlock *search_pb = NULL, *fixup_pb = NULL;
  1823. Slapi_Entry **entries = NULL;
  1824. task_data *td = NULL;
  1825. PRCList *list = NULL;
  1826. int result = 0;
  1827. int i = 0;
  1828. if (!task) {
  1829. return; /* no task */
  1830. }
  1831. slapi_task_inc_refcount(task);
  1832. slapi_log_error( SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1833. "automember_rebuild_task_thread --> refcount incremented.\n" );
  1834. /*
  1835. * Fetch our task data from the task
  1836. */
  1837. td = (task_data *)slapi_task_get_data(task);
  1838. slapi_task_begin(task, 1);
  1839. slapi_task_log_notice(task, "Automember rebuild task starting (base dn: (%s) filter (%s)...\n",
  1840. slapi_sdn_get_dn(td->base_dn),td->filter_str);
  1841. slapi_task_log_status(task, "Automember rebuild task starting (base dn: (%s) filter (%s)...\n",
  1842. slapi_sdn_get_dn(td->base_dn),td->filter_str);
  1843. /*
  1844. * Set the bind dn in the local thread data
  1845. */
  1846. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  1847. /*
  1848. * Search the database
  1849. */
  1850. search_pb = slapi_pblock_new();
  1851. slapi_search_internal_set_pb_ext(search_pb, td->base_dn, td->scope, td->filter_str, NULL,
  1852. 0, NULL, NULL, automember_get_plugin_id(), 0);
  1853. slapi_search_internal_pb(search_pb);
  1854. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1855. if (LDAP_SUCCESS != result){
  1856. slapi_task_log_notice(task, "Automember rebuild membership task unable to search"
  1857. " on base (%s) filter (%s) error (%d)\n", slapi_sdn_get_dn(td->base_dn),
  1858. td->filter_str, result);
  1859. slapi_task_log_status(task, "Automember rebuild membership task unable to search"
  1860. " on base (%s) filter (%s) error (%d)\n", slapi_sdn_get_dn(td->base_dn),
  1861. td->filter_str, result);
  1862. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1863. "Task: unable to search on base (%s) filter (%s) error (%d)\n",
  1864. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  1865. goto out;
  1866. }
  1867. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1868. /*
  1869. * If this is a backend txn plugin, start the transaction
  1870. */
  1871. if (plugin_is_betxn) {
  1872. Slapi_Backend *be = slapi_be_select(td->base_dn);
  1873. if (be) {
  1874. fixup_pb = slapi_pblock_new();
  1875. slapi_pblock_set(fixup_pb, SLAPI_BACKEND, be);
  1876. if(slapi_back_transaction_begin(fixup_pb) != LDAP_SUCCESS){
  1877. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1878. "automember_rebuild_task_thread: failed to start transaction\n");
  1879. }
  1880. } else {
  1881. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1882. "automember_rebuild_task_thread: failed to get be backend from %s\n",
  1883. slapi_sdn_get_dn(td->base_dn));
  1884. }
  1885. }
  1886. /*
  1887. * Grab the config read lock, and loop over the entries
  1888. */
  1889. automember_config_read_lock();
  1890. for (i = 0; entries && (entries[i] != NULL); i++){
  1891. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  1892. list = PR_LIST_HEAD(g_automember_config);
  1893. while (list != g_automember_config) {
  1894. config = (struct configEntry *)list;
  1895. /* Does the entry meet scope and filter requirements? */
  1896. if (slapi_dn_issuffix(slapi_entry_get_dn(entries[i]), config->scope) &&
  1897. (slapi_filter_test_simple(entries[i], config->filter) == 0))
  1898. {
  1899. if (slapi_is_shutting_down() ||
  1900. automember_update_membership(config, entries[i], NULL)) {
  1901. result = SLAPI_PLUGIN_FAILURE;
  1902. automember_config_unlock();
  1903. goto out;
  1904. }
  1905. }
  1906. list = PR_NEXT_LINK(list);
  1907. }
  1908. }
  1909. }
  1910. automember_config_unlock();
  1911. out:
  1912. if (plugin_is_betxn && fixup_pb) {
  1913. if (i == 0 || result != 0) { /* no updates performed */
  1914. slapi_back_transaction_abort(fixup_pb);
  1915. } else {
  1916. slapi_back_transaction_commit(fixup_pb);
  1917. }
  1918. slapi_pblock_destroy(fixup_pb);
  1919. }
  1920. slapi_free_search_results_internal(search_pb);
  1921. slapi_pblock_destroy(search_pb);
  1922. if(result){
  1923. /* error */
  1924. slapi_task_log_notice(task, "Automember rebuild task aborted. Error (%d)", result);
  1925. slapi_task_log_status(task, "Automember rebuild task aborted. Error (%d)", result);
  1926. } else {
  1927. slapi_task_log_notice(task, "Automember rebuild task finished. Processed (%d) entries.", i);
  1928. slapi_task_log_status(task, "Automember rebuild task finished. Processed (%d) entries.", i);
  1929. }
  1930. slapi_task_inc_progress(task);
  1931. slapi_task_finish(task, result);
  1932. slapi_task_dec_refcount(task);
  1933. slapi_log_error( SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1934. "automember_rebuild_task_thread <-- refcount decremented.\n" );
  1935. }
  1936. /*
  1937. * Export an ldif of the changes that would be made if we ran the automember rebuild membership task
  1938. *
  1939. * task entry:
  1940. *
  1941. * dn: cn=my export task, cn=automember export updates,cn=tasks,cn=config
  1942. * objectClass: top
  1943. * objectClass: extensibleObject
  1944. * cn: my export task
  1945. * basedn: dc=example,dc=com
  1946. * filter: (uid=*)
  1947. * scope: sub
  1948. * ldif: /tmp/automem-updates.ldif
  1949. */
  1950. static int
  1951. automember_task_add_export_updates(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1952. int *returncode, char *returntext, void *arg)
  1953. {
  1954. int rv = SLAPI_DSE_CALLBACK_OK;
  1955. task_data *mytaskdata = NULL;
  1956. Slapi_Task *task = NULL;
  1957. PRThread *thread = NULL;
  1958. char *bind_dn = NULL;
  1959. const char *base_dn = NULL;
  1960. const char *filter = NULL;
  1961. const char *ldif = NULL;
  1962. const char *scope = NULL;
  1963. *returncode = LDAP_SUCCESS;
  1964. if((ldif = fetch_attr(e, "ldif", 0)) == NULL){
  1965. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1966. rv = SLAPI_DSE_CALLBACK_ERROR;
  1967. goto out;
  1968. }
  1969. if((base_dn = fetch_attr(e, "basedn", 0)) == NULL){
  1970. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1971. rv = SLAPI_DSE_CALLBACK_ERROR;
  1972. goto out;
  1973. }
  1974. if((filter = fetch_attr(e, "filter", 0)) == NULL){
  1975. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1976. rv = SLAPI_DSE_CALLBACK_ERROR;
  1977. goto out;
  1978. }
  1979. scope = fetch_attr(e, "scope", "sub");
  1980. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  1981. mytaskdata = (task_data*)slapi_ch_calloc(1, sizeof(task_data));
  1982. if (mytaskdata == NULL){
  1983. *returncode = LDAP_OPERATIONS_ERROR;
  1984. rv = SLAPI_DSE_CALLBACK_ERROR;
  1985. goto out;
  1986. }
  1987. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  1988. mytaskdata->ldif_out = slapi_ch_strdup(ldif);
  1989. mytaskdata->base_dn = slapi_sdn_new_dn_byval(base_dn);
  1990. mytaskdata->filter_str = slapi_ch_strdup(filter);
  1991. if(scope){
  1992. if(strcasecmp(scope,"sub")== 0){
  1993. mytaskdata->scope = 2;
  1994. } else if(strcasecmp(scope,"one")== 0){
  1995. mytaskdata->scope = 1;
  1996. } else if(strcasecmp(scope,"base")== 0){
  1997. mytaskdata->scope = 0;
  1998. } else {
  1999. /* Hmm, possible typo, use subtree */
  2000. mytaskdata->scope = 2;
  2001. }
  2002. } else {
  2003. /* subtree by default */
  2004. mytaskdata->scope = 2;
  2005. }
  2006. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  2007. slapi_task_set_destructor_fn(task, automember_task_export_destructor);
  2008. slapi_task_set_data(task, mytaskdata);
  2009. /*
  2010. * Start the task as a separate thread
  2011. */
  2012. thread = PR_CreateThread(PR_USER_THREAD, automember_export_task_thread,
  2013. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2014. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2015. if (thread == NULL){
  2016. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2017. "unable to create export task thread!\n");
  2018. *returncode = LDAP_OPERATIONS_ERROR;
  2019. rv = SLAPI_DSE_CALLBACK_ERROR;
  2020. slapi_task_finish(task, *returncode);
  2021. } else {
  2022. rv = SLAPI_DSE_CALLBACK_OK;
  2023. }
  2024. out:
  2025. return rv;
  2026. }
  2027. void automember_export_task_thread(void *arg)
  2028. {
  2029. Slapi_Task *task = (Slapi_Task *)arg;
  2030. Slapi_PBlock *search_pb = NULL;
  2031. Slapi_Entry **entries = NULL;
  2032. int result = SLAPI_DSE_CALLBACK_OK;
  2033. struct configEntry *config = NULL;
  2034. PRCList *list = NULL;
  2035. task_data *td = NULL;
  2036. PRFileDesc *ldif_fd;
  2037. int i = 0;
  2038. int rc = 0;
  2039. if (!task) {
  2040. return; /* no task */
  2041. }
  2042. slapi_task_inc_refcount(task);
  2043. slapi_log_error( SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2044. "automember_export_task_thread --> refcount incremented.\n" );
  2045. td = (task_data *)slapi_task_get_data(task);
  2046. slapi_task_begin(task, 1);
  2047. slapi_task_log_notice(task, "Automember export task starting. Exporting changes to (%s)", td->ldif_out);
  2048. slapi_task_log_status(task, "Automember export task starting. Exporting changes to (%s)", td->ldif_out);
  2049. /* make sure we can open the ldif file */
  2050. if (( ldif_fd = PR_Open( td->ldif_out, PR_CREATE_FILE | PR_WRONLY, DEFAULT_FILE_MODE )) == NULL ){
  2051. rc = PR_GetOSError();
  2052. slapi_task_log_notice(task, "Automember export task could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2053. td->ldif_out, rc, slapi_system_strerror(rc));
  2054. slapi_task_log_status(task, "Automember export task could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2055. td->ldif_out, rc, slapi_system_strerror(rc) );
  2056. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2057. "Could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2058. td->ldif_out, rc, slapi_system_strerror(rc) );
  2059. result = SLAPI_DSE_CALLBACK_ERROR;
  2060. goto out;
  2061. }
  2062. /*
  2063. * Set the bind dn in the local thread data
  2064. */
  2065. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  2066. /*
  2067. * Search the database
  2068. */
  2069. search_pb = slapi_pblock_new();
  2070. slapi_search_internal_set_pb_ext(search_pb, td->base_dn, td->scope, td->filter_str, NULL,
  2071. 0, NULL, NULL, automember_get_plugin_id(), 0);
  2072. slapi_search_internal_pb(search_pb);
  2073. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2074. if (LDAP_SUCCESS != result){
  2075. slapi_task_log_notice(task, "Automember task failed to search on base (%s) filter (%s) error (%d)\n",
  2076. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2077. slapi_task_log_status(task, "Automember task failed to search on base (%s) filter (%s) error (%d)\n",
  2078. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2079. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2080. "Task: unable to search on base (%s) filter (%s) error (%d)\n",
  2081. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2082. goto out;
  2083. }
  2084. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2085. /*
  2086. * Grab the config read lock, and loop over the entries
  2087. */
  2088. automember_config_read_lock();
  2089. for (i = 0; entries && (entries[i] != NULL); i++){
  2090. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2091. list = PR_LIST_HEAD(g_automember_config);
  2092. while (list != g_automember_config) {
  2093. config = (struct configEntry *)list;
  2094. if (slapi_dn_issuffix(slapi_sdn_get_dn(td->base_dn), config->scope) &&
  2095. (slapi_filter_test_simple(entries[i], config->filter) == 0))
  2096. {
  2097. if (slapi_is_shutting_down() ||
  2098. automember_update_membership(config, entries[i], ldif_fd)) {
  2099. result = SLAPI_DSE_CALLBACK_ERROR;
  2100. automember_config_unlock();
  2101. goto out;
  2102. }
  2103. }
  2104. list = PR_NEXT_LINK(list);
  2105. }
  2106. }
  2107. }
  2108. automember_config_unlock();
  2109. out:
  2110. slapi_free_search_results_internal(search_pb);
  2111. slapi_pblock_destroy(search_pb);
  2112. if(ldif_fd){
  2113. PR_Close(ldif_fd);
  2114. }
  2115. if(result){
  2116. /* error */
  2117. slapi_task_log_notice(task, "Automember export task aborted. Error (%d)", result);
  2118. slapi_task_log_status(task, "Automember export task aborted. Error (%d)", result);
  2119. } else {
  2120. slapi_task_log_notice(task, "Automember export task finished. Processed (%d) entries.", i);
  2121. slapi_task_log_status(task, "Automember export task finished. Processed (%d) entries.", i);
  2122. }
  2123. slapi_task_inc_progress(task);
  2124. slapi_task_finish(task, result);
  2125. slapi_task_dec_refcount(task);
  2126. slapi_log_error( SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2127. "automember_export_task_thread <-- refcount decremented.\n" );
  2128. }
  2129. /*
  2130. * Export an ldif of the changes that would be made from the entries
  2131. * in the provided ldif file
  2132. *
  2133. * task entry:
  2134. *
  2135. * dn: cn=my map task, cn=automember map updates,cn=tasks,cn=config
  2136. * objectClass: top
  2137. * objectClass: extensibleObject
  2138. * cn: my export task
  2139. * ldif_in: /tmp/entries.ldif
  2140. * ldif_out: /tmp/automem-updates.ldif
  2141. */
  2142. static int
  2143. automember_task_add_map_entries(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2144. int *returncode, char *returntext, void *arg)
  2145. {
  2146. int rv = SLAPI_DSE_CALLBACK_OK;
  2147. task_data *mytaskdata = NULL;
  2148. Slapi_Task *task = NULL;
  2149. PRThread *thread = NULL;
  2150. char *bind_dn;
  2151. const char *ldif_out;
  2152. const char *ldif_in;
  2153. *returncode = LDAP_SUCCESS;
  2154. /*
  2155. * Get the params
  2156. */
  2157. if((ldif_in = fetch_attr(e, "ldif_in", 0)) == NULL){
  2158. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2159. rv = SLAPI_DSE_CALLBACK_ERROR;
  2160. goto out;
  2161. }
  2162. if((ldif_out = fetch_attr(e, "ldif_out", 0)) == NULL){
  2163. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2164. rv = SLAPI_DSE_CALLBACK_ERROR;
  2165. goto out;
  2166. }
  2167. /*
  2168. * Setup the task data
  2169. */
  2170. mytaskdata = (task_data*)slapi_ch_calloc(1, sizeof(task_data));
  2171. if (mytaskdata == NULL){
  2172. *returncode = LDAP_OPERATIONS_ERROR;
  2173. rv = SLAPI_DSE_CALLBACK_ERROR;
  2174. goto out;
  2175. }
  2176. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  2177. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  2178. mytaskdata->ldif_out = slapi_ch_strdup(ldif_out);
  2179. mytaskdata->ldif_in = slapi_ch_strdup(ldif_in);
  2180. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  2181. slapi_task_set_destructor_fn(task, automember_task_map_destructor);
  2182. slapi_task_set_data(task, mytaskdata);
  2183. /*
  2184. * Start the task as a separate thread
  2185. */
  2186. thread = PR_CreateThread(PR_USER_THREAD, automember_map_task_thread,
  2187. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2188. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2189. if (thread == NULL){
  2190. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2191. "unable to create map task thread!\n");
  2192. *returncode = LDAP_OPERATIONS_ERROR;
  2193. rv = SLAPI_DSE_CALLBACK_ERROR;
  2194. slapi_task_finish(task, *returncode);
  2195. } else {
  2196. rv = SLAPI_DSE_CALLBACK_OK;
  2197. }
  2198. out:
  2199. return rv;
  2200. }
  2201. /*
  2202. * Read in the text entries from ldif_in, and convert them to slapi_entries.
  2203. * Then, write to ldif_out what the updates would be if these entries were added
  2204. */
  2205. void automember_map_task_thread(void *arg)
  2206. {
  2207. Slapi_Task *task = (Slapi_Task *)arg;
  2208. Slapi_Entry *e = NULL;
  2209. int result = SLAPI_DSE_CALLBACK_OK;
  2210. struct configEntry *config = NULL;
  2211. PRCList *list = NULL;
  2212. task_data *td = NULL;
  2213. PRFileDesc *ldif_fd_out = NULL;
  2214. char *entrystr = NULL;
  2215. char *errstr = NULL;
  2216. #if defined(USE_OPENLDAP)
  2217. int buflen = 0;
  2218. LDIFFP *ldif_fd_in = NULL;
  2219. ldif_record_lineno_t lineno = 0;
  2220. #else
  2221. FILE *ldif_fd_in = NULL;
  2222. int lineno = 0;
  2223. #endif
  2224. int rc = 0;
  2225. if (!task) {
  2226. return; /* no task */
  2227. }
  2228. slapi_task_inc_refcount(task);
  2229. slapi_log_error( SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2230. "automember_map_task_thread --> refcount incremented.\n" );
  2231. td = (task_data *)slapi_task_get_data(task);
  2232. slapi_task_begin(task, 1);
  2233. slapi_task_log_notice(task, "Automember map task starting... Reading entries from (%s)"
  2234. " and writing the updates to (%s)",td->ldif_in, td->ldif_out);
  2235. slapi_task_log_status(task, "Automember map task starting... Reading entries from (%s)"
  2236. " and writing the updates to (%s)",td->ldif_in, td->ldif_out);
  2237. /* make sure we can open the ldif files */
  2238. if(( ldif_fd_out = PR_Open( td->ldif_out, PR_CREATE_FILE | PR_WRONLY, DEFAULT_FILE_MODE )) == NULL ){
  2239. rc = PR_GetOSError();
  2240. slapi_task_log_notice(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2241. td->ldif_out, rc, slapi_system_strerror(rc));
  2242. slapi_task_log_status(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2243. td->ldif_out, rc, slapi_system_strerror(rc));
  2244. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2245. "Could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2246. td->ldif_out, rc, slapi_system_strerror(rc) );
  2247. result = SLAPI_DSE_CALLBACK_ERROR;
  2248. goto out;
  2249. }
  2250. #if defined(USE_OPENLDAP)
  2251. if(( ldif_fd_in = ldif_open(td->ldif_in, "r")) == NULL ){
  2252. rc = errno;
  2253. errstr = strerror(rc);
  2254. #else
  2255. if(( ldif_fd_in = fopen( td->ldif_in, "r")) == NULL ){
  2256. rc = PR_GetOSError();
  2257. errstr = (char *)slapi_system_strerror(rc);
  2258. #endif
  2259. slapi_task_log_notice(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2260. td->ldif_in, rc, errstr);
  2261. slapi_task_log_status(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2262. td->ldif_in, rc, errstr);
  2263. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2264. "Could not open ldif file \"%s\" for reading, error %d (%s)\n",
  2265. td->ldif_in, rc, errstr );
  2266. result = SLAPI_DSE_CALLBACK_ERROR;
  2267. goto out;
  2268. }
  2269. /*
  2270. * Convert each LDIF entry to a slapi_entry
  2271. */
  2272. automember_config_read_lock();
  2273. #if defined(USE_OPENLDAP)
  2274. while (ldif_read_record(ldif_fd_in, &lineno, &entrystr, &buflen)){
  2275. buflen = 0;
  2276. #else
  2277. while ((entrystr = ldif_get_entry(ldif_fd_in, &lineno)) != NULL){
  2278. #endif
  2279. e = slapi_str2entry( entrystr, 0 );
  2280. if ( e != NULL ){
  2281. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2282. list = PR_LIST_HEAD(g_automember_config);
  2283. while (list != g_automember_config) {
  2284. config = (struct configEntry *)list;
  2285. if (slapi_dn_issuffix(slapi_entry_get_dn_const(e), config->scope) &&
  2286. (slapi_filter_test_simple(e, config->filter) == 0))
  2287. {
  2288. if (slapi_is_shutting_down() ||
  2289. automember_update_membership(config, e, ldif_fd_out)) {
  2290. result = SLAPI_DSE_CALLBACK_ERROR;
  2291. slapi_entry_free(e);
  2292. slapi_ch_free_string(&entrystr);
  2293. automember_config_unlock();
  2294. goto out;
  2295. }
  2296. }
  2297. list = PR_NEXT_LINK(list);
  2298. }
  2299. }
  2300. slapi_entry_free(e);
  2301. } else {
  2302. /* invalid entry */
  2303. slapi_task_log_notice(task, "Automember map task, skipping invalid entry.");
  2304. slapi_task_log_status(task, "Automember map task, skipping invalid entry.");
  2305. }
  2306. slapi_ch_free_string(&entrystr);
  2307. }
  2308. automember_config_unlock();
  2309. out:
  2310. if(ldif_fd_out){
  2311. PR_Close(ldif_fd_out);
  2312. }
  2313. if(ldif_fd_in){
  2314. #if defined(USE_OPENLDAP)
  2315. ldif_close(ldif_fd_in);
  2316. #else
  2317. fclose(ldif_fd_in);
  2318. #endif
  2319. }
  2320. slapi_task_inc_progress(task);
  2321. slapi_task_finish(task, result);
  2322. slapi_task_dec_refcount(task);
  2323. slapi_log_error( SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2324. "automember_map_task_thread <-- refcount decremented.\n" );
  2325. }
  2326. /*
  2327. * automember_modrdn_post_op()
  2328. *
  2329. * Reloads config if config entries move
  2330. * into or out of our config scope.
  2331. */
  2332. static int
  2333. automember_modrdn_post_op(Slapi_PBlock *pb)
  2334. {
  2335. Slapi_Entry *post_e = NULL;
  2336. Slapi_DN *old_sdn = NULL;
  2337. Slapi_DN *new_sdn = NULL;
  2338. struct configEntry *config = NULL;
  2339. PRCList *list = NULL;
  2340. int rc = SLAPI_PLUGIN_SUCCESS;
  2341. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2342. "--> automember_modrdn_post_op\n");
  2343. /* Just bail if we aren't ready to service requests yet. */
  2344. if (!automember_oktodo(pb)){
  2345. return SLAPI_PLUGIN_SUCCESS;
  2346. }
  2347. /*
  2348. * Reload config if an existing config entry was renamed,
  2349. * or if the new dn brings an entry into the scope of the
  2350. * config entries.
  2351. */
  2352. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  2353. if (post_e) {
  2354. new_sdn = slapi_entry_get_sdn(post_e);
  2355. } else {
  2356. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2357. "automember_modrdn_post_op: Error "
  2358. "retrieving post-op entry\n");
  2359. return SLAPI_PLUGIN_FAILURE;
  2360. }
  2361. if ((old_sdn = automember_get_sdn(pb))) {
  2362. if (automember_dn_is_config(old_sdn) || automember_dn_is_config(new_sdn)){
  2363. automember_load_config();
  2364. }
  2365. } else {
  2366. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2367. "automember_modrdn_post_op: Error "
  2368. "retrieving dn\n");
  2369. return SLAPI_PLUGIN_FAILURE;
  2370. }
  2371. /* If replication, just bail. */
  2372. if (automember_isrepl(pb)) {
  2373. return SLAPI_PLUGIN_SUCCESS;
  2374. }
  2375. /*
  2376. * Check if a config entry applies to the entry(post modrdn)
  2377. */
  2378. automember_config_read_lock();
  2379. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2380. list = PR_LIST_HEAD(g_automember_config);
  2381. while (list != g_automember_config) {
  2382. config = (struct configEntry *)list;
  2383. /* Does the entry meet scope and filter requirements? */
  2384. if (slapi_dn_issuffix(slapi_sdn_get_dn(new_sdn), config->scope) &&
  2385. (slapi_filter_test_simple(post_e, config->filter) == 0)) {
  2386. /* Find out what membership changes are needed and make them. */
  2387. if(automember_update_membership(config, post_e, NULL)){
  2388. rc = SLAPI_PLUGIN_FAILURE;
  2389. break;
  2390. }
  2391. }
  2392. list = PR_NEXT_LINK(list);
  2393. }
  2394. }
  2395. automember_config_unlock();
  2396. if(rc){
  2397. char errtxt[SLAPI_DSE_RETURNTEXT_SIZE];
  2398. int result = LDAP_UNWILLING_TO_PERFORM;
  2399. PR_snprintf(errtxt, SLAPI_DSE_RETURNTEXT_SIZE, "Automember Plugin update unexpectedly failed. "
  2400. "Please see the server errors log for more information.\n");
  2401. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &result);
  2402. slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, &errtxt);
  2403. }
  2404. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2405. "<-- automember_modrdn_post_op (%d)\n", rc);
  2406. return rc;
  2407. }