automember.c 98 KB

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