automember.c 95 KB

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