memberof.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK
  8. **/
  9. /* The memberof plugin updates the memberof attribute of entries
  10. * based on modifications performed on groupofuniquenames entries
  11. *
  12. * In addition the plugin provides a DS task that may be started
  13. * administrative clients and that creates the initial memberof
  14. * list for imported entries and/or fixes the memberof list of
  15. * existing entries that have inconsistent state (for example,
  16. * if the memberof attribute was incorrectly edited directly)
  17. *
  18. * To start the memberof task add an entry like:
  19. *
  20. * dn: cn=mytask, cn=memberof task, cn=tasks, cn=config
  21. * objectClass: top
  22. * objectClass: extensibleObject
  23. * cn: mytask
  24. * basedn: dc=example, dc=com
  25. * filter: (uid=test4)
  26. *
  27. * where "basedn" is required and refers to the top most node to perform the
  28. * task on, and where "filter" is an optional attribute that provides a filter
  29. * describing the entries to be worked on
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include "slapi-plugin.h"
  35. #include "string.h"
  36. #include "nspr.h"
  37. #include "memberof.h"
  38. static Slapi_PluginDesc pdesc = { "memberof", VENDOR,
  39. DS_PACKAGE_VERSION, "memberof plugin" };
  40. static void* _PluginID = NULL;
  41. static Slapi_DN* _ConfigAreaDN = NULL;
  42. static Slapi_RWLock *config_rwlock = NULL;
  43. static Slapi_DN* _pluginDN = NULL;
  44. static PRMonitor *memberof_operation_lock = 0;
  45. MemberOfConfig *qsortConfig = 0;
  46. static int usetxn = 0;
  47. static int premodfn = 0;
  48. typedef struct _memberofstringll
  49. {
  50. const char *dn;
  51. void *next;
  52. } memberofstringll;
  53. typedef struct _memberof_get_groups_data
  54. {
  55. MemberOfConfig *config;
  56. Slapi_Value *memberdn_val;
  57. Slapi_ValueSet **groupvals;
  58. Slapi_ValueSet **group_norm_vals;
  59. } memberof_get_groups_data;
  60. /*** function prototypes ***/
  61. /* exported functions */
  62. int memberof_postop_init(Slapi_PBlock *pb );
  63. static int memberof_internal_postop_init(Slapi_PBlock *pb);
  64. static int memberof_preop_init(Slapi_PBlock *pb);
  65. /* plugin callbacks */
  66. static int memberof_postop_del(Slapi_PBlock *pb );
  67. static int memberof_postop_modrdn(Slapi_PBlock *pb );
  68. static int memberof_postop_modify(Slapi_PBlock *pb );
  69. static int memberof_postop_add(Slapi_PBlock *pb );
  70. static int memberof_postop_start(Slapi_PBlock *pb);
  71. static int memberof_postop_close(Slapi_PBlock *pb);
  72. /* supporting cast */
  73. static int memberof_oktodo(Slapi_PBlock *pb);
  74. static Slapi_DN *memberof_getsdn(Slapi_PBlock *pb);
  75. static int memberof_modop_one(Slapi_PBlock *pb, MemberOfConfig *config, int mod_op,
  76. Slapi_DN *op_this_sdn, Slapi_DN *op_to_sdn);
  77. static int memberof_modop_one_r(Slapi_PBlock *pb, MemberOfConfig *config, int mod_op,
  78. Slapi_DN *group_sdn, Slapi_DN *op_this_sdn, Slapi_DN *op_to_sdn,
  79. memberofstringll *stack);
  80. static int memberof_add_one(Slapi_PBlock *pb, MemberOfConfig *config,
  81. Slapi_DN *addthis_sdn, Slapi_DN *addto_sdn);
  82. static int memberof_del_one(Slapi_PBlock *pb, MemberOfConfig *config,
  83. Slapi_DN *delthis_sdn, Slapi_DN *delfrom_sdn);
  84. static int memberof_mod_smod_list(Slapi_PBlock *pb, MemberOfConfig *config,
  85. int mod, Slapi_DN *group_sdn, Slapi_Mod *smod);
  86. static int memberof_add_smod_list(Slapi_PBlock *pb, MemberOfConfig *config,
  87. Slapi_DN *group_sdn, Slapi_Mod *smod);
  88. static int memberof_del_smod_list(Slapi_PBlock *pb, MemberOfConfig *config,
  89. Slapi_DN *group_sdn, Slapi_Mod *smod);
  90. static int memberof_mod_attr_list(Slapi_PBlock *pb, MemberOfConfig *config, int mod,
  91. Slapi_DN *group_sdn, Slapi_Attr *attr);
  92. static int memberof_mod_attr_list_r(Slapi_PBlock *pb, MemberOfConfig *config,
  93. int mod, Slapi_DN *group_sdn, Slapi_DN *op_this_sdn, Slapi_Attr *attr,
  94. memberofstringll *stack);
  95. static int memberof_add_attr_list(Slapi_PBlock *pb, MemberOfConfig *config,
  96. Slapi_DN *group_sdn, Slapi_Attr *attr);
  97. static int memberof_del_attr_list(Slapi_PBlock *pb, MemberOfConfig *config,
  98. Slapi_DN *group_sdn, Slapi_Attr *attr);
  99. static int memberof_moddn_attr_list(Slapi_PBlock *pb, MemberOfConfig *config,
  100. Slapi_DN *pre_sdn, Slapi_DN *post_sdn, Slapi_Attr *attr);
  101. static int memberof_replace_list(Slapi_PBlock *pb, MemberOfConfig *config,
  102. Slapi_DN *group_sdn);
  103. static void memberof_set_plugin_id(void * plugin_id);
  104. static int memberof_compare(MemberOfConfig *config, const void *a, const void *b);
  105. static int memberof_qsort_compare(const void *a, const void *b);
  106. static void memberof_load_array(Slapi_Value **array, Slapi_Attr *attr);
  107. static int memberof_del_dn_from_groups(Slapi_PBlock *pb, MemberOfConfig *config, Slapi_DN *sdn);
  108. static int memberof_call_foreach_dn(Slapi_PBlock *pb, Slapi_DN *sdn, MemberOfConfig *config,
  109. char **types, plugin_search_entry_callback callback, void *callback_data);
  110. static int memberof_is_direct_member(MemberOfConfig *config, Slapi_Value *groupdn,
  111. Slapi_Value *memberdn);
  112. static int memberof_is_grouping_attr(char *type, MemberOfConfig *config);
  113. static Slapi_ValueSet *memberof_get_groups(MemberOfConfig *config, Slapi_DN *member_sdn);
  114. static int memberof_get_groups_r(MemberOfConfig *config, Slapi_DN *member_sdn,
  115. memberof_get_groups_data *data);
  116. static int memberof_get_groups_callback(Slapi_Entry *e, void *callback_data);
  117. static int memberof_test_membership(Slapi_PBlock *pb, MemberOfConfig *config,
  118. Slapi_DN *group_sdn);
  119. static int memberof_test_membership_callback(Slapi_Entry *e, void *callback_data);
  120. static int memberof_del_dn_type_callback(Slapi_Entry *e, void *callback_data);
  121. static int memberof_replace_dn_type_callback(Slapi_Entry *e, void *callback_data);
  122. static int memberof_replace_dn_from_groups(Slapi_PBlock *pb, MemberOfConfig *config,
  123. Slapi_DN *pre_sdn, Slapi_DN *post_sdn);
  124. static int memberof_modop_one_replace_r(Slapi_PBlock *pb, MemberOfConfig *config,
  125. int mod_op, Slapi_DN *group_sdn, Slapi_DN *op_this_sdn,
  126. Slapi_DN *replace_with_sdn, Slapi_DN *op_to_sdn, memberofstringll *stack);
  127. static int memberof_task_add(Slapi_PBlock *pb, Slapi_Entry *e,
  128. Slapi_Entry *eAfter, int *returncode, char *returntext,
  129. void *arg);
  130. static void memberof_task_destructor(Slapi_Task *task);
  131. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  132. const char *default_val);
  133. static void memberof_fixup_task_thread(void *arg);
  134. static int memberof_fix_memberof(MemberOfConfig *config, char *dn, char *filter_str);
  135. static int memberof_fix_memberof_callback(Slapi_Entry *e, void *callback_data);
  136. static int memberof_entry_in_scope(MemberOfConfig *config, Slapi_DN *sdn);
  137. static int memberof_add_objectclass(char *auto_add_oc, const char *dn);
  138. static int memberof_add_memberof_attr(LDAPMod **mods, const char *dn, char *add_oc);
  139. /*** implementation ***/
  140. /*** exported functions ***/
  141. /*
  142. * memberof_postop_init()
  143. *
  144. * Register plugin call backs
  145. *
  146. */
  147. int
  148. memberof_postop_init(Slapi_PBlock *pb)
  149. {
  150. int ret = 0;
  151. char *memberof_plugin_identity = 0;
  152. Slapi_Entry *plugin_entry = NULL;
  153. char *plugin_type = NULL;
  154. char *preop_plugin_type = NULL;
  155. int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
  156. int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
  157. int modfn = SLAPI_PLUGIN_POST_MODIFY_FN;
  158. int addfn = SLAPI_PLUGIN_POST_ADD_FN;
  159. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  160. "--> memberof_postop_init\n" );
  161. /* get args */
  162. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  163. plugin_entry &&
  164. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  165. plugin_type && strstr(plugin_type, "betxn")) {
  166. usetxn = 1;
  167. delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  168. mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  169. modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  170. addfn = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  171. }
  172. slapi_ch_free_string(&plugin_type);
  173. if(usetxn){
  174. preop_plugin_type = "betxnpreoperation";
  175. premodfn = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  176. } else {
  177. preop_plugin_type = "preoperation";
  178. premodfn = SLAPI_PLUGIN_PRE_MODIFY_FN;
  179. }
  180. if(plugin_entry){
  181. memberof_set_plugin_area(slapi_entry_get_sdn(plugin_entry));
  182. }
  183. /*
  184. * Get plugin identity and stored it for later use
  185. * Used for internal operations
  186. */
  187. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &memberof_plugin_identity);
  188. PR_ASSERT (memberof_plugin_identity);
  189. memberof_set_plugin_id(memberof_plugin_identity);
  190. ret = ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  191. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, (void *)&pdesc ) != 0 ||
  192. slapi_pblock_set( pb, delfn, (void *) memberof_postop_del ) != 0 ||
  193. slapi_pblock_set( pb, mdnfn, (void *) memberof_postop_modrdn ) != 0 ||
  194. slapi_pblock_set( pb, modfn, (void *) memberof_postop_modify ) != 0 ||
  195. slapi_pblock_set( pb, addfn, (void *) memberof_postop_add ) != 0 ||
  196. slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN, (void *) memberof_postop_start ) != 0 ||
  197. slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN, (void *) memberof_postop_close ) != 0 );
  198. if (!ret && !usetxn &&
  199. slapi_register_plugin("internalpostoperation", /* op type */
  200. 1, /* Enabled */
  201. "memberof_postop_init", /* this function desc */
  202. memberof_internal_postop_init, /* init func */
  203. MEMBEROF_INT_PREOP_DESC, /* plugin desc */
  204. NULL, /* ? */
  205. memberof_plugin_identity /* access control */))
  206. {
  207. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  208. "memberof_postop_init failed\n" );
  209. ret = -1;
  210. }
  211. else if (ret)
  212. {
  213. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  214. "memberof_postop_init failed\n" );
  215. ret = -1;
  216. }
  217. /*
  218. * Setup the preop plugin for shared config updates
  219. */
  220. if (!ret && slapi_register_plugin(preop_plugin_type, /* op type */
  221. 1, /* Enabled */
  222. "memberof_preop_init", /* this function desc */
  223. memberof_preop_init, /* init func */
  224. MEMBEROF_PREOP_DESC, /* plugin desc */
  225. NULL, /* ? */
  226. memberof_plugin_identity /* access control */))
  227. {
  228. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  229. "memberof_preop_init failed\n" );
  230. ret = -1;
  231. }
  232. else if (ret)
  233. {
  234. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  235. "memberof_preop_init failed\n");
  236. ret = -1;
  237. }
  238. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  239. "<-- memberof_postop_init\n" );
  240. return ret;
  241. }
  242. static int
  243. memberof_preop_init(Slapi_PBlock *pb)
  244. {
  245. int status = 0;
  246. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01) != 0 ||
  247. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, (void *) &pdesc) != 0 ||
  248. slapi_pblock_set(pb, premodfn, (void *)memberof_shared_config_validate) != 0)
  249. {
  250. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  251. "memberof_internal_postop_init: failed to register plugin\n");
  252. status = -1;
  253. }
  254. return status;
  255. }
  256. static int
  257. memberof_internal_postop_init(Slapi_PBlock *pb)
  258. {
  259. int status = 0;
  260. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  261. SLAPI_PLUGIN_VERSION_01) != 0 ||
  262. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  263. (void *) &pdesc) != 0 ||
  264. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  265. (void *) memberof_postop_del) != 0 ||
  266. slapi_pblock_set( pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  267. (void *) memberof_postop_modrdn ) != 0 ||
  268. slapi_pblock_set( pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  269. (void *) memberof_postop_modify ) != 0 ||
  270. slapi_pblock_set( pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  271. (void *) memberof_postop_add ) != 0) {
  272. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  273. "memberof_internal_postop_init: failed to register plugin\n");
  274. status = -1;
  275. }
  276. return status;
  277. }
  278. /*
  279. * memberof_postop_start()
  280. *
  281. * Do plugin start up stuff
  282. *
  283. */
  284. int memberof_postop_start(Slapi_PBlock *pb)
  285. {
  286. Slapi_PBlock *search_pb = NULL;
  287. Slapi_Entry **entries = NULL;
  288. Slapi_Entry *config_e = NULL; /* entry containing plugin config */
  289. char *config_area = NULL;
  290. int result = 0;
  291. int rc = 0;
  292. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  293. "--> memberof_postop_start\n" );
  294. memberof_operation_lock = PR_NewMonitor();
  295. if(0 == memberof_operation_lock)
  296. {
  297. rc = -1;
  298. goto bail;
  299. }
  300. if(config_rwlock == NULL){
  301. if((config_rwlock = slapi_new_rwlock()) == NULL){
  302. rc = -1;
  303. goto bail;
  304. }
  305. }
  306. /* Set the alternate config area if one is defined. */
  307. slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
  308. if (config_area)
  309. {
  310. search_pb = slapi_pblock_new();
  311. slapi_search_internal_set_pb(search_pb, config_area, LDAP_SCOPE_BASE, "objectclass=*",
  312. NULL, 0, NULL, NULL, memberof_get_plugin_id(), 0);
  313. slapi_search_internal_pb(search_pb);
  314. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  315. if (LDAP_SUCCESS != result) {
  316. if (result == LDAP_NO_SUCH_OBJECT) {
  317. /* log an error and use the plugin entry for the config */
  318. slapi_log_error(SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  319. "memberof_postop_start: Config entry \"%s\" does "
  320. "not exist.\n", config_area);
  321. rc = -1;
  322. goto bail;
  323. }
  324. } else {
  325. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  326. if(entries && entries[0]){
  327. config_e = entries[0];
  328. } else {
  329. slapi_log_error(SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  330. "memberof_postop_start: Config entry \"%s\" was "
  331. "not located.\n", config_area);
  332. rc = -1;
  333. goto bail;
  334. }
  335. }
  336. } else {
  337. /* The plugin entry itself contains the config */
  338. if ( slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &config_e ) != 0 ) {
  339. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  340. "missing config entry\n" );
  341. rc = -1;
  342. goto bail;
  343. }
  344. }
  345. memberof_set_config_area(slapi_entry_get_sdn(config_e));
  346. if (( rc = memberof_config( config_e, pb )) != LDAP_SUCCESS ) {
  347. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  348. "configuration failed (%s)\n", ldap_err2string( rc ));
  349. rc = -1;
  350. goto bail;
  351. }
  352. rc = slapi_plugin_task_register_handler("memberof task", memberof_task_add, pb);
  353. if(rc)
  354. {
  355. goto bail;
  356. }
  357. /*
  358. * TODO: start up operation actor thread
  359. * need to get to a point where server failure
  360. * or shutdown doesn't hose our operations
  361. * so we should create a task entry that contains
  362. * all required information to complete the operation
  363. * then the tasks can be restarted safely if
  364. * interrupted
  365. */
  366. bail:
  367. slapi_free_search_results_internal(search_pb);
  368. slapi_pblock_destroy(search_pb);
  369. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  370. "<-- memberof_postop_start\n" );
  371. return rc;
  372. }
  373. /*
  374. * memberof_postop_close()
  375. *
  376. * Do plugin shut down stuff
  377. *
  378. */
  379. int memberof_postop_close(Slapi_PBlock *pb)
  380. {
  381. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  382. "--> memberof_postop_close\n" );
  383. slapi_plugin_task_unregister_handler("memberof task", memberof_task_add);
  384. memberof_release_config();
  385. slapi_sdn_free(&_ConfigAreaDN);
  386. slapi_sdn_free(&_pluginDN);
  387. slapi_destroy_rwlock(config_rwlock);
  388. config_rwlock = NULL;
  389. PR_DestroyMonitor(memberof_operation_lock);
  390. memberof_operation_lock = NULL;
  391. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  392. "<-- memberof_postop_close\n" );
  393. return 0;
  394. }
  395. void
  396. memberof_set_config_area(Slapi_DN *dn)
  397. {
  398. slapi_rwlock_wrlock(config_rwlock);
  399. slapi_sdn_free(&_ConfigAreaDN);
  400. _ConfigAreaDN = slapi_sdn_dup(dn);
  401. slapi_rwlock_unlock(config_rwlock);
  402. }
  403. Slapi_DN *
  404. memberof_get_config_area()
  405. {
  406. return _ConfigAreaDN;
  407. }
  408. int
  409. memberof_sdn_config_cmp(Slapi_DN *sdn)
  410. {
  411. int rc = 0;
  412. slapi_rwlock_rdlock(config_rwlock);
  413. rc = slapi_sdn_compare(sdn, memberof_get_config_area());
  414. slapi_rwlock_unlock(config_rwlock);
  415. return rc;
  416. }
  417. void
  418. memberof_set_plugin_area(Slapi_DN *sdn)
  419. {
  420. slapi_sdn_free(&_pluginDN);
  421. _pluginDN = slapi_sdn_dup(sdn);
  422. }
  423. Slapi_DN *
  424. memberof_get_plugin_area()
  425. {
  426. return _pluginDN;
  427. }
  428. /*
  429. * memberof_postop_del()
  430. *
  431. * All entries with a memberOf attribute that contains the group DN get retrieved
  432. * and have the their memberOf attribute regenerated (it is far too complex and
  433. * error prone to attempt to change only those dn values involved in this case -
  434. * mainly because the deleted group may itself be a member of other groups which
  435. * may be members of other groups etc. in a big recursive mess involving dependency
  436. * chains that must be created and traversed in order to decide if an entry should
  437. * really have those groups removed too)
  438. */
  439. int memberof_postop_del(Slapi_PBlock *pb)
  440. {
  441. int ret = SLAPI_PLUGIN_SUCCESS;
  442. MemberOfConfig *mainConfig = NULL;
  443. MemberOfConfig configCopy = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  444. Slapi_DN *sdn;
  445. void *caller_id = NULL;
  446. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  447. "--> memberof_postop_del\n" );
  448. /* We don't want to process internal modify
  449. * operations that originate from this plugin. */
  450. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  451. if (caller_id == memberof_get_plugin_id()) {
  452. /* Just return without processing */
  453. return SLAPI_PLUGIN_SUCCESS;
  454. }
  455. if(memberof_oktodo(pb) && (sdn = memberof_getsdn(pb)))
  456. {
  457. struct slapi_entry *e = NULL;
  458. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &e );
  459. memberof_rlock_config();
  460. mainConfig = memberof_get_config();
  461. if(!memberof_entry_in_scope(mainConfig, slapi_entry_get_sdn(e))){
  462. /* The entry is not in scope, bail...*/
  463. memberof_unlock_config();
  464. goto bail;
  465. }
  466. memberof_copy_config(&configCopy, memberof_get_config());
  467. memberof_unlock_config();
  468. /* get the memberOf operation lock */
  469. memberof_lock();
  470. /* remove this DN from the
  471. * membership lists of groups
  472. */
  473. if((ret = memberof_del_dn_from_groups(pb, &configCopy, sdn))){
  474. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  475. "memberof_postop_del: error deleting dn (%s) from group. Error (%d)\n",
  476. slapi_sdn_get_dn(sdn),ret);
  477. memberof_unlock();
  478. goto bail;
  479. }
  480. /* is the entry of interest as a group? */
  481. if(e && configCopy.group_filter && 0 == slapi_filter_test_simple(e, configCopy.group_filter))
  482. {
  483. int i = 0;
  484. Slapi_Attr *attr = 0;
  485. /* Loop through to find each grouping attribute separately. */
  486. for (i = 0; configCopy.groupattrs[i] && ret == LDAP_SUCCESS; i++)
  487. {
  488. if (0 == slapi_entry_attr_find(e, configCopy.groupattrs[i], &attr))
  489. {
  490. if((ret = memberof_del_attr_list(pb, &configCopy, sdn, attr))){
  491. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  492. "memberof_postop_del: error deleting attr list - dn (%s). Error (%d)\n",
  493. slapi_sdn_get_dn(sdn),ret);
  494. }
  495. }
  496. }
  497. }
  498. memberof_unlock();
  499. bail:
  500. memberof_free_config(&configCopy);
  501. }
  502. if(ret){
  503. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  504. ret = SLAPI_PLUGIN_FAILURE;
  505. }
  506. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  507. "<-- memberof_postop_del\n" );
  508. return ret;
  509. }
  510. typedef struct _memberof_del_dn_data
  511. {
  512. char *dn;
  513. char *type;
  514. } memberof_del_dn_data;
  515. /* Deletes a member dn from all groups that refer to it. */
  516. static int
  517. memberof_del_dn_from_groups(Slapi_PBlock *pb, MemberOfConfig *config, Slapi_DN *sdn)
  518. {
  519. int i = 0;
  520. char *groupattrs[2] = {0, 0};
  521. int rc = LDAP_SUCCESS;
  522. /* Loop through each grouping attribute to find groups that have
  523. * dn as a member. For any matches, delete the dn value from the
  524. * same grouping attribute. */
  525. for (i = 0; config->groupattrs && config->groupattrs[i] && rc == LDAP_SUCCESS; i++)
  526. {
  527. memberof_del_dn_data data = {(char *)slapi_sdn_get_dn(sdn),
  528. config->groupattrs[i]};
  529. groupattrs[0] = config->groupattrs[i];
  530. rc = memberof_call_foreach_dn(pb, sdn, config, groupattrs,
  531. memberof_del_dn_type_callback, &data);
  532. }
  533. return rc;
  534. }
  535. int
  536. memberof_del_dn_type_callback(Slapi_Entry *e, void *callback_data)
  537. {
  538. int rc = 0;
  539. LDAPMod mod;
  540. LDAPMod *mods[2];
  541. char *val[2];
  542. Slapi_PBlock *mod_pb = 0;
  543. mod_pb = slapi_pblock_new();
  544. mods[0] = &mod;
  545. mods[1] = 0;
  546. val[0] = ((memberof_del_dn_data *)callback_data)->dn;
  547. val[1] = 0;
  548. mod.mod_op = LDAP_MOD_DELETE;
  549. mod.mod_type = ((memberof_del_dn_data *)callback_data)->type;
  550. mod.mod_values = val;
  551. slapi_modify_internal_set_pb_ext(
  552. mod_pb, slapi_entry_get_sdn(e),
  553. mods, 0, 0,
  554. memberof_get_plugin_id(), 0);
  555. slapi_modify_internal_pb(mod_pb);
  556. slapi_pblock_get(mod_pb,
  557. SLAPI_PLUGIN_INTOP_RESULT,
  558. &rc);
  559. slapi_pblock_destroy(mod_pb);
  560. if (rc == LDAP_NO_SUCH_ATTRIBUTE && val[0] == NULL) {
  561. /* if no memberof attribut exists
  562. * handle as success
  563. */
  564. rc = LDAP_SUCCESS;
  565. }
  566. return rc;
  567. }
  568. /* Check if the the entry include scope is a child of the sdn */
  569. static Slapi_DN*
  570. memberof_scope_is_child_of_dn(MemberOfConfig *config, Slapi_DN *sdn)
  571. {
  572. int i = 0;
  573. while(config->entryScopes && config->entryScopes[i]){
  574. if(slapi_sdn_issuffix(config->entryScopes[i], sdn)){
  575. return config->entryScopes[i];
  576. }
  577. i++;
  578. }
  579. return NULL;
  580. }
  581. /*
  582. * Does a callback search of "type=dn" under the db suffix that "dn" is in,
  583. * unless all_backends is set, then we look at all the backends. If "dn"
  584. * is a user, you'd want "type" to be "member". If "dn" is a group, you
  585. * could want type to be either "member" or "memberOf" depending on the case.
  586. */
  587. int
  588. memberof_call_foreach_dn(Slapi_PBlock *pb, Slapi_DN *sdn,
  589. MemberOfConfig *config, char **types, plugin_search_entry_callback callback, void *callback_data)
  590. {
  591. Slapi_PBlock *search_pb = NULL;
  592. Slapi_DN *base_sdn = NULL;
  593. Slapi_Backend *be = NULL;
  594. char *escaped_filter_val;
  595. char *filter_str = NULL;
  596. char *cookie = NULL;
  597. int all_backends = config->allBackends;
  598. int types_name_len = 0;
  599. int num_types = 0;
  600. int dn_len = slapi_sdn_get_ndn_len(sdn);
  601. int free_it = 0;
  602. int rc = 0;
  603. int i = 0;
  604. if (!memberof_entry_in_scope(config, sdn)) {
  605. return (rc);
  606. }
  607. /* Count the number of types. */
  608. for (num_types = 0; types && types[num_types]; num_types++)
  609. {
  610. /* Add up the total length of all attribute names.
  611. * We need to know this for building the filter. */
  612. types_name_len += strlen(types[num_types]);
  613. }
  614. /* Escape the dn, and build the search filter. */
  615. escaped_filter_val = slapi_escape_filter_value((char *)slapi_sdn_get_dn(sdn), dn_len);
  616. if(escaped_filter_val){
  617. dn_len = strlen(escaped_filter_val);
  618. free_it = 1;
  619. } else {
  620. escaped_filter_val = (char *)slapi_sdn_get_dn(sdn);
  621. }
  622. if (num_types > 1)
  623. {
  624. int bytes_out = 0;
  625. int filter_str_len = types_name_len + (num_types * (3 + dn_len)) + 4;
  626. /* Allocate enough space for the filter */
  627. filter_str = slapi_ch_malloc(filter_str_len);
  628. /* Add beginning of filter. */
  629. bytes_out = snprintf(filter_str, filter_str_len - bytes_out, "(|");
  630. /* Add filter section for each type. */
  631. for (i = 0; types[i]; i++)
  632. {
  633. bytes_out += snprintf(filter_str + bytes_out, filter_str_len - bytes_out,
  634. "(%s=%s)", types[i], escaped_filter_val);
  635. }
  636. /* Add end of filter. */
  637. snprintf(filter_str + bytes_out, filter_str_len - bytes_out, ")");
  638. }
  639. else if (num_types == 1)
  640. {
  641. filter_str = slapi_ch_smprintf("(%s=%s)", types[0], escaped_filter_val);
  642. }
  643. if(free_it)
  644. slapi_ch_free_string(&escaped_filter_val);
  645. if(filter_str == NULL){
  646. return rc;
  647. }
  648. search_pb = slapi_pblock_new();
  649. be = slapi_get_first_backend(&cookie);
  650. while(be){
  651. Slapi_DN *scope_sdn = NULL;
  652. if(!all_backends){
  653. be = slapi_be_select(sdn);
  654. if(be == NULL){
  655. break;
  656. }
  657. }
  658. if((base_sdn = (Slapi_DN *)slapi_be_getsuffix(be,0)) == NULL){
  659. if(!all_backends){
  660. break;
  661. } else {
  662. /* its ok, goto the next backend */
  663. be = slapi_get_next_backend(cookie);
  664. continue;
  665. }
  666. }
  667. if (config->entryScopes || config->entryScopeExcludeSubtrees) {
  668. if (memberof_entry_in_scope(config, base_sdn)) {
  669. /* do nothing, entry scope is spanning
  670. * multiple suffixes, start at suffix */
  671. } else if ((scope_sdn = memberof_scope_is_child_of_dn(config, base_sdn))) {
  672. /* scope is below suffix, set search base */
  673. base_sdn = scope_sdn;
  674. } else if(!all_backends){
  675. break;
  676. } else {
  677. /* its ok, goto the next backend */
  678. be = slapi_get_next_backend(cookie);
  679. continue;
  680. }
  681. }
  682. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_dn(base_sdn),
  683. LDAP_SCOPE_SUBTREE, filter_str, 0, 0, 0, 0, memberof_get_plugin_id(), 0);
  684. slapi_search_internal_callback_pb(search_pb, callback_data, 0, callback, 0);
  685. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  686. if(rc != LDAP_SUCCESS){
  687. break;
  688. }
  689. if(!all_backends){
  690. break;
  691. }
  692. slapi_pblock_init(search_pb);
  693. be = slapi_get_next_backend(cookie);
  694. }
  695. slapi_pblock_destroy(search_pb);
  696. slapi_ch_free((void **)&cookie);
  697. slapi_ch_free_string(&filter_str);
  698. return rc;
  699. }
  700. /*
  701. * memberof_postop_modrdn()
  702. *
  703. * All entries with a memberOf attribute that contains the old group DN get retrieved
  704. * and have the old group DN deleted and the new group DN added to their memberOf attribute
  705. */
  706. int memberof_postop_modrdn(Slapi_PBlock *pb)
  707. {
  708. int ret = SLAPI_PLUGIN_SUCCESS;
  709. void *caller_id = NULL;
  710. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  711. "--> memberof_postop_modrdn\n" );
  712. /* We don't want to process internal modify
  713. * operations that originate from this plugin. */
  714. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  715. if (caller_id == memberof_get_plugin_id()) {
  716. /* Just return without processing */
  717. return SLAPI_PLUGIN_SUCCESS;
  718. }
  719. if(memberof_oktodo(pb))
  720. {
  721. MemberOfConfig *mainConfig = 0;
  722. MemberOfConfig configCopy = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  723. struct slapi_entry *pre_e = NULL;
  724. struct slapi_entry *post_e = NULL;
  725. Slapi_DN *pre_sdn = 0;
  726. Slapi_DN *post_sdn = 0;
  727. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &pre_e );
  728. slapi_pblock_get( pb, SLAPI_ENTRY_POST_OP, &post_e );
  729. if(pre_e && post_e)
  730. {
  731. pre_sdn = slapi_entry_get_sdn(pre_e);
  732. post_sdn = slapi_entry_get_sdn(post_e);
  733. }
  734. /* copy config so it doesn't change out from under us */
  735. memberof_rlock_config();
  736. mainConfig = memberof_get_config();
  737. memberof_copy_config(&configCopy, mainConfig);
  738. memberof_unlock_config();
  739. /* Need to check both the pre/post entries */
  740. if((pre_sdn && !memberof_entry_in_scope(&configCopy, pre_sdn)) &&
  741. (post_sdn && !memberof_entry_in_scope(&configCopy, post_sdn)))
  742. {
  743. /* The entry is not in scope */
  744. goto bail;
  745. }
  746. memberof_lock();
  747. /* update any downstream members */
  748. if(pre_sdn && post_sdn && configCopy.group_filter &&
  749. 0 == slapi_filter_test_simple(post_e, configCopy.group_filter))
  750. {
  751. int i = 0;
  752. Slapi_Attr *attr = 0;
  753. /* get a list of member attributes present in the group
  754. * entry that is being renamed. */
  755. for (i = 0; configCopy.groupattrs[i]; i++)
  756. {
  757. if(0 == slapi_entry_attr_find(post_e, configCopy.groupattrs[i], &attr))
  758. {
  759. if((ret = memberof_moddn_attr_list(pb, &configCopy, pre_sdn,
  760. post_sdn, attr) != 0))
  761. {
  762. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  763. "memberof_postop_modrdn - update failed for (%s), error (%d)\n",
  764. slapi_sdn_get_dn(pre_sdn), ret);
  765. break;
  766. }
  767. }
  768. }
  769. }
  770. /* It's possible that this is an entry who is a member
  771. * of other group entries. We need to update any member
  772. * attributes to refer to the new name. */
  773. if (ret == LDAP_SUCCESS && pre_sdn && post_sdn) {
  774. if (!memberof_entry_in_scope(&configCopy, post_sdn)){
  775. if((ret = memberof_del_dn_from_groups(pb, &configCopy, pre_sdn))){
  776. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  777. "memberof_postop_modrdn - delete dn failed for (%s), error (%d)\n",
  778. slapi_sdn_get_dn(pre_sdn), ret);
  779. }
  780. if(ret == LDAP_SUCCESS && pre_e && configCopy.group_filter &&
  781. 0 == slapi_filter_test_simple(pre_e, configCopy.group_filter))
  782. {
  783. /* is the entry of interest as a group? */
  784. int i = 0;
  785. Slapi_Attr *attr = 0;
  786. /* Loop through to find each grouping attribute separately. */
  787. for (i = 0; configCopy.groupattrs[i] && ret == LDAP_SUCCESS; i++) {
  788. if (0 == slapi_entry_attr_find(pre_e, configCopy.groupattrs[i], &attr)) {
  789. if((ret = memberof_del_attr_list(pb, &configCopy, pre_sdn, attr))){
  790. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  791. "memberof_postop_modrdn: error deleting attr list - dn (%s). Error (%d)\n",
  792. slapi_sdn_get_dn(pre_sdn),ret);
  793. }
  794. }
  795. }
  796. }
  797. if(ret == LDAP_SUCCESS) {
  798. memberof_del_dn_data del_data = {0, configCopy.memberof_attr};
  799. if((ret = memberof_del_dn_type_callback(post_e, &del_data))){
  800. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  801. "memberof_postop_modrdn - delete dn callback failed for (%s), error (%d)\n",
  802. slapi_entry_get_dn(post_e), ret);
  803. }
  804. }
  805. } else {
  806. if((ret = memberof_replace_dn_from_groups(pb, &configCopy, pre_sdn, post_sdn))){
  807. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  808. "memberof_postop_modrdn - replace dn failed for (%s), error (%d)\n",
  809. slapi_sdn_get_dn(pre_sdn), ret);
  810. }
  811. }
  812. }
  813. memberof_unlock();
  814. bail:
  815. memberof_free_config(&configCopy);
  816. }
  817. if(ret){
  818. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  819. ret = SLAPI_PLUGIN_FAILURE;
  820. }
  821. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  822. "<-- memberof_postop_modrdn\n" );
  823. return ret;
  824. }
  825. typedef struct _replace_dn_data
  826. {
  827. char *pre_dn;
  828. char *post_dn;
  829. char *type;
  830. char *add_oc;
  831. } replace_dn_data;
  832. /* Finds any groups that have pre_dn as a member and modifies them to
  833. * to use post_dn instead. */
  834. static int
  835. memberof_replace_dn_from_groups(Slapi_PBlock *pb, MemberOfConfig *config,
  836. Slapi_DN *pre_sdn, Slapi_DN *post_sdn)
  837. {
  838. int i = 0;
  839. char *groupattrs[2] = {0, 0};
  840. int ret = LDAP_SUCCESS;
  841. /* Loop through each grouping attribute to find groups that have
  842. * pre_dn as a member. For any matches, replace pre_dn with post_dn
  843. * using the same grouping attribute. */
  844. for (i = 0; config->groupattrs && config->groupattrs[i]; i++)
  845. {
  846. replace_dn_data data = {(char *)slapi_sdn_get_dn(pre_sdn),
  847. (char *)slapi_sdn_get_dn(post_sdn),
  848. config->groupattrs[i],
  849. config->auto_add_oc
  850. };
  851. groupattrs[0] = config->groupattrs[i];
  852. if((ret = memberof_call_foreach_dn(pb, pre_sdn, config, groupattrs,
  853. memberof_replace_dn_type_callback,
  854. &data)))
  855. {
  856. break;
  857. }
  858. }
  859. return ret;
  860. }
  861. int memberof_replace_dn_type_callback(Slapi_Entry *e, void *callback_data)
  862. {
  863. int rc = 0;
  864. LDAPMod delmod;
  865. LDAPMod addmod;
  866. LDAPMod *mods[3];
  867. char *delval[2];
  868. char *addval[2];
  869. char *dn = NULL;
  870. dn = slapi_entry_get_dn(e);
  871. mods[0] = &delmod;
  872. mods[1] = &addmod;
  873. mods[2] = 0;
  874. delval[0] = ((replace_dn_data *)callback_data)->pre_dn;
  875. delval[1] = 0;
  876. delmod.mod_op = LDAP_MOD_DELETE;
  877. delmod.mod_type = ((replace_dn_data *)callback_data)->type;
  878. delmod.mod_values = delval;
  879. addval[0] = ((replace_dn_data *)callback_data)->post_dn;
  880. addval[1] = 0;
  881. addmod.mod_op = LDAP_MOD_ADD;
  882. addmod.mod_type = ((replace_dn_data *)callback_data)->type;
  883. addmod.mod_values = addval;
  884. rc = memberof_add_memberof_attr(mods, dn,
  885. ((replace_dn_data *)callback_data)->add_oc);
  886. return rc;
  887. }
  888. /*
  889. * memberof_postop_modify()
  890. *
  891. * Added members are retrieved and have the group DN added to their memberOf attribute
  892. * Deleted members are retrieved and have the group DN deleted from their memberOf attribute
  893. * On replace of the membership attribute values:
  894. * 1. Sort old and new values
  895. * 2. Iterate through both lists at same time
  896. * 3. Any value not in old list but in new list - add group DN to memberOf attribute
  897. * 4. Any value in old list but not in new list - remove group DN from memberOf attribute
  898. *
  899. * Note: this will suck for large groups but nonetheless is optimal (it's linear) given
  900. * current restrictions i.e. originally adding members in sorted order would allow
  901. * us to sort one list only (the new one) but that is under server control, not this plugin
  902. */
  903. int memberof_postop_modify(Slapi_PBlock *pb)
  904. {
  905. int ret = SLAPI_PLUGIN_SUCCESS;
  906. Slapi_DN *sdn = NULL;
  907. Slapi_Mods *smods = 0;
  908. Slapi_Mod *smod = 0;
  909. LDAPMod **mods;
  910. Slapi_Mod *next_mod = 0;
  911. void *caller_id = NULL;
  912. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  913. "--> memberof_postop_modify\n" );
  914. /* We don't want to process internal modify
  915. * operations that originate from this plugin. */
  916. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  917. if (caller_id == memberof_get_plugin_id()) {
  918. /* Just return without processing */
  919. return SLAPI_PLUGIN_SUCCESS;
  920. }
  921. /* check if we are updating the shared config entry */
  922. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  923. if (memberof_sdn_config_cmp(sdn) == 0)
  924. {
  925. Slapi_Entry *entry = NULL;
  926. char returntext[SLAPI_DSE_RETURNTEXT_SIZE];
  927. int result = 0;
  928. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &entry);
  929. if(entry){
  930. if( SLAPI_DSE_CALLBACK_ERROR == memberof_apply_config (pb, NULL, entry, &result, returntext, NULL)){
  931. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM, "%s", returntext);
  932. ret = SLAPI_PLUGIN_FAILURE;
  933. goto done;
  934. }
  935. } else {
  936. /* this should not happen since this was validated in preop */
  937. ret = SLAPI_PLUGIN_FAILURE;
  938. goto done;
  939. }
  940. /* we're done, no need to do the other processing */
  941. goto done;
  942. }
  943. if(memberof_oktodo(pb))
  944. {
  945. int config_copied = 0;
  946. MemberOfConfig *mainConfig = 0;
  947. MemberOfConfig configCopy = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  948. /* get the mod set */
  949. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  950. smods = slapi_mods_new();
  951. slapi_mods_init_byref(smods, mods);
  952. next_mod = slapi_mod_new();
  953. smod = slapi_mods_get_first_smod(smods, next_mod);
  954. while(smod)
  955. {
  956. int interested = 0;
  957. char *type = (char *)slapi_mod_get_type(smod);
  958. /* We only want to copy the config if we encounter an
  959. * operation that we need to act on. We also want to
  960. * only copy the config the first time it's needed so
  961. * it remains the same for all mods in the operation,
  962. * despite any config changes that may be made. */
  963. if (!config_copied){
  964. memberof_rlock_config();
  965. mainConfig = memberof_get_config();
  966. if (memberof_is_grouping_attr(type, mainConfig))
  967. {
  968. interested = 1;
  969. if (!memberof_entry_in_scope(mainConfig, sdn)){
  970. /* Entry is not in scope */
  971. memberof_unlock_config();
  972. goto bail;
  973. }
  974. /* copy config so it doesn't change out from under us */
  975. memberof_copy_config(&configCopy, mainConfig);
  976. config_copied = 1;
  977. }
  978. memberof_unlock_config();
  979. } else {
  980. if (memberof_is_grouping_attr(type, &configCopy))
  981. {
  982. interested = 1;
  983. }
  984. }
  985. if(interested)
  986. {
  987. int op = slapi_mod_get_operation(smod);
  988. memberof_lock();
  989. /* the modify op decides the function */
  990. switch(op & ~LDAP_MOD_BVALUES)
  991. {
  992. case LDAP_MOD_ADD:
  993. {
  994. /* add group DN to targets */
  995. if((ret = memberof_add_smod_list(pb, &configCopy, sdn, smod))){
  996. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  997. "memberof_postop_modify: failed to add dn (%s) to target. "
  998. "Error (%d)\n", slapi_sdn_get_dn(sdn), ret );
  999. slapi_mod_done(next_mod);
  1000. memberof_unlock();
  1001. goto bail;
  1002. }
  1003. break;
  1004. }
  1005. case LDAP_MOD_DELETE:
  1006. {
  1007. /* If there are no values in the smod, we should
  1008. * just do a replace instead. The user is just
  1009. * trying to delete all members from this group
  1010. * entry, which the replace code deals with. */
  1011. if (slapi_mod_get_num_values(smod) == 0)
  1012. {
  1013. if((ret = memberof_replace_list(pb, &configCopy, sdn))){
  1014. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1015. "memberof_postop_modify: failed to replace list (%s). "
  1016. "Error (%d)\n", slapi_sdn_get_dn(sdn), ret );
  1017. slapi_mod_done(next_mod);
  1018. memberof_unlock();
  1019. goto bail;
  1020. }
  1021. }
  1022. else
  1023. {
  1024. /* remove group DN from target values in smod*/
  1025. if((ret = memberof_del_smod_list(pb, &configCopy, sdn, smod))){
  1026. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1027. "memberof_postop_modify: failed to remove dn (%s). "
  1028. "Error (%d)\n", slapi_sdn_get_dn(sdn), ret );
  1029. slapi_mod_done(next_mod);
  1030. memberof_unlock();
  1031. goto bail;
  1032. }
  1033. }
  1034. break;
  1035. }
  1036. case LDAP_MOD_REPLACE:
  1037. {
  1038. /* replace current values */
  1039. if((ret = memberof_replace_list(pb, &configCopy, sdn))){
  1040. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1041. "memberof_postop_modify: failed to replace values in dn (%s). "
  1042. "Error (%d)\n", slapi_sdn_get_dn(sdn), ret );
  1043. slapi_mod_done(next_mod);
  1044. memberof_unlock();
  1045. goto bail;
  1046. }
  1047. break;
  1048. }
  1049. default:
  1050. {
  1051. slapi_log_error(
  1052. SLAPI_LOG_FATAL,
  1053. MEMBEROF_PLUGIN_SUBSYSTEM,
  1054. "memberof_postop_modify: unknown mod type\n" );
  1055. ret = SLAPI_PLUGIN_FAILURE;
  1056. break;
  1057. }
  1058. }
  1059. memberof_unlock();
  1060. }
  1061. slapi_mod_done(next_mod);
  1062. smod = slapi_mods_get_next_smod(smods, next_mod);
  1063. }
  1064. bail:
  1065. if (config_copied){
  1066. memberof_free_config(&configCopy);
  1067. }
  1068. slapi_mod_free(&next_mod);
  1069. slapi_mods_free(&smods);
  1070. }
  1071. done:
  1072. if(ret){
  1073. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  1074. ret = SLAPI_PLUGIN_FAILURE;
  1075. }
  1076. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  1077. "<-- memberof_postop_modify\n" );
  1078. return ret;
  1079. }
  1080. /*
  1081. * memberof_postop_add()
  1082. *
  1083. * All members in the membership attribute of the new entry get retrieved
  1084. * and have the group DN added to their memberOf attribute
  1085. */
  1086. int memberof_postop_add(Slapi_PBlock *pb)
  1087. {
  1088. int ret = SLAPI_PLUGIN_SUCCESS;
  1089. int interested = 0;
  1090. Slapi_DN *sdn = 0;
  1091. void *caller_id = NULL;
  1092. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  1093. "--> memberof_postop_add\n" );
  1094. /* We don't want to process internal modify
  1095. * operations that originate from this plugin. */
  1096. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  1097. if (caller_id == memberof_get_plugin_id()) {
  1098. /* Just return without processing */
  1099. return SLAPI_PLUGIN_SUCCESS;
  1100. }
  1101. if(memberof_oktodo(pb) && (sdn = memberof_getsdn(pb)))
  1102. {
  1103. struct slapi_entry *e = NULL;
  1104. MemberOfConfig configCopy = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1105. MemberOfConfig *mainConfig;
  1106. slapi_pblock_get( pb, SLAPI_ENTRY_POST_OP, &e );
  1107. /* is the entry of interest? */
  1108. memberof_rlock_config();
  1109. mainConfig = memberof_get_config();
  1110. if(e && mainConfig && mainConfig->group_filter &&
  1111. 0 == slapi_filter_test_simple(e, mainConfig->group_filter))
  1112. {
  1113. interested = 1;
  1114. if(!memberof_entry_in_scope(mainConfig, slapi_entry_get_sdn(e))){
  1115. /* Entry is not in scope */
  1116. memberof_unlock_config();
  1117. goto bail;
  1118. }
  1119. memberof_copy_config(&configCopy, memberof_get_config());
  1120. }
  1121. memberof_unlock_config();
  1122. if(interested)
  1123. {
  1124. int i = 0;
  1125. Slapi_Attr *attr = 0;
  1126. memberof_lock();
  1127. for (i = 0; configCopy.groupattrs && configCopy.groupattrs[i]; i++)
  1128. {
  1129. if(0 == slapi_entry_attr_find(e, configCopy.groupattrs[i], &attr))
  1130. {
  1131. if((ret = memberof_add_attr_list(pb, &configCopy, sdn, attr))){
  1132. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1133. "memberof_postop_add: failed to add dn(%s), error (%d)\n",
  1134. slapi_sdn_get_dn(sdn), ret);
  1135. break;
  1136. }
  1137. }
  1138. }
  1139. memberof_unlock();
  1140. memberof_free_config(&configCopy);
  1141. }
  1142. }
  1143. bail:
  1144. if(ret){
  1145. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  1146. ret = SLAPI_PLUGIN_FAILURE;
  1147. }
  1148. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  1149. "<-- memberof_postop_add\n" );
  1150. return ret;
  1151. }
  1152. /*** Support functions ***/
  1153. /*
  1154. * memberof_oktodo()
  1155. *
  1156. * Check that the op succeeded
  1157. * Note: we also respond to replicated ops so we don't test for that
  1158. * this does require that the memberOf attribute not be replicated
  1159. * and this means that memberof is consistent with local state
  1160. * not the network system state
  1161. *
  1162. */
  1163. int memberof_oktodo(Slapi_PBlock *pb)
  1164. {
  1165. int ret = 1;
  1166. int oprc = 0;
  1167. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  1168. "--> memberof_postop_oktodo\n" );
  1169. if (!slapi_plugin_running(pb)) {
  1170. ret = 0;
  1171. goto bail;
  1172. }
  1173. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0)
  1174. {
  1175. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1176. "memberof_postop_oktodo: could not get parameters\n" );
  1177. ret = -1;
  1178. }
  1179. /* this plugin should only execute if the operation succeeded */
  1180. if(oprc != 0)
  1181. {
  1182. ret = 0;
  1183. }
  1184. bail:
  1185. slapi_log_error( SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
  1186. "<-- memberof_postop_oktodo\n" );
  1187. return ret;
  1188. }
  1189. /*
  1190. * Return 1 if the entry is in the scope.
  1191. * For MODRDN the caller should check both the preop
  1192. * and postop entries. If we are moving out of, or
  1193. * into scope, we should process it.
  1194. */
  1195. static int
  1196. memberof_entry_in_scope(MemberOfConfig *config, Slapi_DN *sdn)
  1197. {
  1198. if (config->entryScopeExcludeSubtrees){
  1199. int i = 0;
  1200. /* check the excludes */
  1201. while(config->entryScopeExcludeSubtrees[i]){
  1202. if (slapi_sdn_issuffix(sdn, config->entryScopeExcludeSubtrees[i])){
  1203. return 0;
  1204. }
  1205. i++;
  1206. }
  1207. }
  1208. if (config->entryScopes){
  1209. int i = 0;
  1210. /* check the excludes */
  1211. while(config->entryScopes[i]){
  1212. if (slapi_sdn_issuffix(sdn, config->entryScopes[i])){
  1213. return 1;
  1214. }
  1215. i++;
  1216. }
  1217. return 0;
  1218. }
  1219. return 1;
  1220. }
  1221. static Slapi_DN *
  1222. memberof_getsdn(Slapi_PBlock *pb)
  1223. {
  1224. Slapi_DN *sdn = NULL;
  1225. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  1226. return sdn;
  1227. }
  1228. /*
  1229. * memberof_modop_one()
  1230. *
  1231. * Perform op on memberof attribute of op_to using op_this as the value
  1232. * However, if op_to happens to be a group, we must arrange for the group
  1233. * members to have the mod performed on them instead, and we must take
  1234. * care to not recurse when we have visted a group before
  1235. *
  1236. * Also, we must not delete entries that are a member of the group
  1237. */
  1238. int memberof_modop_one(Slapi_PBlock *pb, MemberOfConfig *config, int mod_op,
  1239. Slapi_DN *op_this_sdn, Slapi_DN *op_to_sdn)
  1240. {
  1241. return memberof_modop_one_r(pb, config, mod_op, op_this_sdn,
  1242. op_this_sdn, op_to_sdn, 0);
  1243. }
  1244. /* memberof_modop_one_r()
  1245. *
  1246. * recursive function to perform above (most things don't need the replace arg)
  1247. */
  1248. int memberof_modop_one_r(Slapi_PBlock *pb, MemberOfConfig *config, int mod_op,
  1249. Slapi_DN *group_sdn, Slapi_DN *op_this_sdn, Slapi_DN *op_to_sdn,
  1250. memberofstringll *stack)
  1251. {
  1252. return memberof_modop_one_replace_r(
  1253. pb, config, mod_op, group_sdn, op_this_sdn, 0, op_to_sdn, stack);
  1254. }
  1255. /* memberof_modop_one_replace_r()
  1256. *
  1257. * recursive function to perform above (with added replace arg)
  1258. */
  1259. int
  1260. memberof_modop_one_replace_r(Slapi_PBlock *pb, MemberOfConfig *config,
  1261. int mod_op, Slapi_DN *group_sdn, Slapi_DN *op_this_sdn,
  1262. Slapi_DN *replace_with_sdn, Slapi_DN *op_to_sdn, memberofstringll *stack)
  1263. {
  1264. int rc = 0;
  1265. LDAPMod mod;
  1266. LDAPMod replace_mod;
  1267. LDAPMod *mods[3];
  1268. char *val[2];
  1269. char *replace_val[2];
  1270. Slapi_Entry *e = 0;
  1271. memberofstringll *ll = 0;
  1272. char *op_str = 0;
  1273. const char *op_to;
  1274. const char *op_this;
  1275. Slapi_Value *to_dn_val = NULL;
  1276. Slapi_Value *this_dn_val = NULL;
  1277. op_to = slapi_sdn_get_ndn(op_to_sdn);
  1278. op_this = slapi_sdn_get_ndn(op_this_sdn);
  1279. /* Make sure we have valid DN's for the group(op_this) and the new member(op_to) */
  1280. if(op_to && op_this){
  1281. to_dn_val = slapi_value_new_string(op_to);
  1282. this_dn_val = slapi_value_new_string(op_this);
  1283. }
  1284. if(to_dn_val == NULL){
  1285. const char *udn = op_to_sdn ? slapi_sdn_get_udn(op_to_sdn) : "";
  1286. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1287. "memberof_modop_one_replace_r: failed to get DN value from "
  1288. "member value (%s)\n", udn);
  1289. goto bail;
  1290. }
  1291. if(this_dn_val == NULL){
  1292. const char *udn = op_this_sdn ? slapi_sdn_get_udn(op_this_sdn) : "";
  1293. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1294. "memberof_modop_one_replace_r: failed to get DN value from"
  1295. "group (%s)\n", udn);
  1296. goto bail;
  1297. }
  1298. /* op_this and op_to are both case-normalized */
  1299. slapi_value_set_flags(this_dn_val, SLAPI_ATTR_FLAG_NORMALIZED_CIS);
  1300. slapi_value_set_flags(to_dn_val, SLAPI_ATTR_FLAG_NORMALIZED_CIS);
  1301. if (config == NULL) {
  1302. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1303. "memberof_modop_one_replace_r: NULL config parameter\n");
  1304. goto bail;
  1305. }
  1306. /* determine if this is a group op or single entry */
  1307. slapi_search_internal_get_entry( op_to_sdn, config->groupattrs,
  1308. &e, memberof_get_plugin_id());
  1309. if(!e)
  1310. {
  1311. /* In the case of a delete, we need to worry about the
  1312. * missing entry being a nested group. There's a small
  1313. * window where another thread may have deleted a nested
  1314. * group that our group_dn entry refers to. This has the
  1315. * potential of us missing some indirect member entries
  1316. * that need to be updated. */
  1317. if(LDAP_MOD_DELETE == mod_op)
  1318. {
  1319. Slapi_PBlock *search_pb = slapi_pblock_new();
  1320. Slapi_DN *base_sdn = 0;
  1321. Slapi_Backend *be = 0;
  1322. char *filter_str;
  1323. char *cookie = NULL;
  1324. int n_entries = 0;
  1325. int all_backends = config->allBackends;
  1326. filter_str = slapi_filter_sprintf("(%s=%s%s)", config->memberof_attr, ESC_NEXT_VAL, op_to);
  1327. be = slapi_get_first_backend(&cookie);
  1328. while(be){
  1329. /*
  1330. * We can't tell for sure if the op_to entry is a
  1331. * user or a group since the entry doesn't exist
  1332. * anymore. We can safely ignore the missing entry
  1333. * if no other entries have a memberOf attribute that
  1334. * points to the missing entry.
  1335. */
  1336. if(!all_backends){
  1337. be = slapi_be_select(op_to_sdn);
  1338. if(be == NULL){
  1339. break;
  1340. }
  1341. }
  1342. if((base_sdn = (Slapi_DN*)slapi_be_getsuffix(be,0)) == NULL){
  1343. if(!all_backends){
  1344. break;
  1345. } else {
  1346. be = slapi_get_next_backend (cookie);
  1347. continue;
  1348. }
  1349. }
  1350. if(filter_str)
  1351. {
  1352. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_dn(base_sdn),
  1353. LDAP_SCOPE_SUBTREE, filter_str, 0, 0, 0, 0,
  1354. memberof_get_plugin_id(), 0);
  1355. if (slapi_search_internal_pb(search_pb))
  1356. {
  1357. /* get result and log an error */
  1358. int res = 0;
  1359. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  1360. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1361. "memberof_modop_one_replace_r: error searching for members: %d\n", res);
  1362. } else {
  1363. slapi_pblock_get(search_pb, SLAPI_NENTRIES, &n_entries);
  1364. if(n_entries > 0)
  1365. {
  1366. /* We want to fixup the membership for the
  1367. * entries that referred to the missing group
  1368. * entry. This will fix the references to
  1369. * the missing group as well as the group
  1370. * represented by op_this. */
  1371. memberof_test_membership(pb, config, op_to_sdn);
  1372. }
  1373. }
  1374. slapi_free_search_results_internal(search_pb);
  1375. }
  1376. slapi_pblock_init(search_pb);
  1377. if(!all_backends){
  1378. break;
  1379. }
  1380. be = slapi_get_next_backend (cookie);
  1381. }
  1382. slapi_pblock_destroy(search_pb);
  1383. slapi_ch_free_string(&filter_str);
  1384. slapi_ch_free((void **)&cookie);
  1385. }
  1386. goto bail;
  1387. }
  1388. if(LDAP_MOD_DELETE == mod_op)
  1389. {
  1390. op_str = "DELETE";
  1391. }
  1392. else if(LDAP_MOD_ADD == mod_op)
  1393. {
  1394. op_str = "ADD";
  1395. }
  1396. else if(LDAP_MOD_REPLACE == mod_op)
  1397. {
  1398. op_str = "REPLACE";
  1399. }
  1400. else
  1401. {
  1402. op_str = "UNKNOWN";
  1403. }
  1404. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  1405. "memberof_modop_one_replace_r: %s %s in %s\n"
  1406. ,op_str, op_this, op_to);
  1407. if(config->group_filter && 0 == slapi_filter_test_simple(e, config->group_filter))
  1408. {
  1409. /* group */
  1410. Slapi_Value *ll_dn_val = 0;
  1411. Slapi_Attr *members = 0;
  1412. int i = 0;
  1413. ll = stack;
  1414. /* have we been here before? */
  1415. while(ll)
  1416. {
  1417. ll_dn_val = slapi_value_new_string(ll->dn);
  1418. /* ll->dn is case-normalized */
  1419. slapi_value_set_flags(ll_dn_val, SLAPI_ATTR_FLAG_NORMALIZED_CIS);
  1420. if(0 == memberof_compare(config, &ll_dn_val, &to_dn_val))
  1421. {
  1422. slapi_value_free(&ll_dn_val);
  1423. /* someone set up infinitely
  1424. recursive groups - bail out */
  1425. slapi_log_error( SLAPI_LOG_PLUGIN,
  1426. MEMBEROF_PLUGIN_SUBSYSTEM,
  1427. "memberof_modop_one_replace_r: group recursion"
  1428. " detected in %s\n"
  1429. ,op_to);
  1430. goto bail;
  1431. }
  1432. slapi_value_free(&ll_dn_val);
  1433. ll = ll->next;
  1434. }
  1435. /* do op on group */
  1436. slapi_log_error( SLAPI_LOG_PLUGIN,
  1437. MEMBEROF_PLUGIN_SUBSYSTEM,
  1438. "memberof_modop_one_replace_r: descending into group %s\n",
  1439. op_to);
  1440. /* Add the nested group's DN to the stack so we can detect loops later. */
  1441. ll = (memberofstringll*)slapi_ch_malloc(sizeof(memberofstringll));
  1442. ll->dn = op_to;
  1443. ll->next = stack;
  1444. /* Go through each grouping attribute one at a time. */
  1445. for (i = 0; config->groupattrs[i]; i++)
  1446. {
  1447. slapi_entry_attr_find( e, config->groupattrs[i], &members );
  1448. if(members)
  1449. {
  1450. if((rc = memberof_mod_attr_list_r(pb, config, mod_op, group_sdn,
  1451. op_this_sdn, members, ll)) != 0){
  1452. goto bail;
  1453. }
  1454. }
  1455. }
  1456. {
  1457. /* crazyness follows:
  1458. * strict-aliasing doesn't like the required cast
  1459. * to void for slapi_ch_free so we are made to
  1460. * juggle to get a normal thing done
  1461. */
  1462. void *pll = ll;
  1463. slapi_ch_free(&pll);
  1464. ll = 0;
  1465. }
  1466. }
  1467. /* continue with operation */
  1468. {
  1469. /* We want to avoid listing a group as a memberOf itself
  1470. * in case someone set up a circular grouping.
  1471. */
  1472. if (0 == memberof_compare(config, &this_dn_val, &to_dn_val))
  1473. {
  1474. const char *strval = "NULL";
  1475. if (this_dn_val) {
  1476. strval = slapi_value_get_string(this_dn_val);
  1477. }
  1478. slapi_log_error( SLAPI_LOG_PLUGIN,
  1479. MEMBEROF_PLUGIN_SUBSYSTEM,
  1480. "memberof_modop_one_replace_r: not processing memberOf "
  1481. "operations on self entry: %s\n", strval);
  1482. goto bail;
  1483. }
  1484. /* For add and del modify operations, we just regenerate the
  1485. * memberOf attribute. */
  1486. if(LDAP_MOD_DELETE == mod_op || LDAP_MOD_ADD == mod_op)
  1487. {
  1488. /* find parent groups and replace our member attr */
  1489. rc = memberof_fix_memberof_callback(e, config);
  1490. } else {
  1491. /* single entry - do mod */
  1492. mods[0] = &mod;
  1493. if(LDAP_MOD_REPLACE == mod_op)
  1494. {
  1495. mods[1] = &replace_mod;
  1496. mods[2] = 0;
  1497. }
  1498. else
  1499. {
  1500. mods[1] = 0;
  1501. }
  1502. val[0] = (char *)op_this;
  1503. val[1] = 0;
  1504. mod.mod_op = LDAP_MOD_REPLACE == mod_op?LDAP_MOD_DELETE:mod_op;
  1505. mod.mod_type = config->memberof_attr;
  1506. mod.mod_values = val;
  1507. if(LDAP_MOD_REPLACE == mod_op)
  1508. {
  1509. replace_val[0] = (char *)slapi_sdn_get_dn(replace_with_sdn);
  1510. replace_val[1] = 0;
  1511. replace_mod.mod_op = LDAP_MOD_ADD;
  1512. replace_mod.mod_type = config->memberof_attr;
  1513. replace_mod.mod_values = replace_val;
  1514. }
  1515. rc = memberof_add_memberof_attr(mods, op_to, config->auto_add_oc);
  1516. }
  1517. }
  1518. bail:
  1519. slapi_value_free(&to_dn_val);
  1520. slapi_value_free(&this_dn_val);
  1521. slapi_entry_free(e);
  1522. return rc;
  1523. }
  1524. /*
  1525. * memberof_add_one()
  1526. *
  1527. * Add addthis DN to the memberof attribute of addto
  1528. *
  1529. */
  1530. int
  1531. memberof_add_one(Slapi_PBlock *pb, MemberOfConfig *config,
  1532. Slapi_DN *addthis_sdn, Slapi_DN *addto_sdn)
  1533. {
  1534. return memberof_modop_one(pb, config, LDAP_MOD_ADD, addthis_sdn, addto_sdn);
  1535. }
  1536. /*
  1537. * memberof_del_one()
  1538. *
  1539. * Delete delthis DN from the memberof attribute of delfrom
  1540. *
  1541. */
  1542. int
  1543. memberof_del_one(Slapi_PBlock *pb, MemberOfConfig *config,
  1544. Slapi_DN *delthis_sdn, Slapi_DN *delfrom_sdn)
  1545. {
  1546. return memberof_modop_one(pb, config, LDAP_MOD_DELETE, delthis_sdn, delfrom_sdn);
  1547. }
  1548. /*
  1549. * memberof_mod_smod_list()
  1550. *
  1551. * Perform mod for group DN to the memberof attribute of the list of targets
  1552. *
  1553. */
  1554. int memberof_mod_smod_list(Slapi_PBlock *pb, MemberOfConfig *config, int mod,
  1555. Slapi_DN *group_sdn, Slapi_Mod *smod)
  1556. {
  1557. int rc = 0;
  1558. struct berval *bv = slapi_mod_get_first_value(smod);
  1559. int last_size = 0;
  1560. char *last_str = 0;
  1561. Slapi_DN *sdn = slapi_sdn_new();
  1562. while(bv)
  1563. {
  1564. char *dn_str = 0;
  1565. if(last_size > bv->bv_len)
  1566. {
  1567. dn_str = last_str;
  1568. }
  1569. else
  1570. {
  1571. int the_size = (bv->bv_len * 2) + 1;
  1572. if(last_str)
  1573. slapi_ch_free_string(&last_str);
  1574. dn_str = (char*)slapi_ch_malloc(the_size);
  1575. last_str = dn_str;
  1576. last_size = the_size;
  1577. }
  1578. memset(dn_str, 0, last_size);
  1579. strncpy(dn_str, bv->bv_val, (size_t)bv->bv_len);
  1580. slapi_sdn_set_dn_byref(sdn, dn_str);
  1581. if((rc = memberof_modop_one(pb, config, mod, group_sdn, sdn))){
  1582. break;
  1583. }
  1584. bv = slapi_mod_get_next_value(smod);
  1585. }
  1586. slapi_sdn_free(&sdn);
  1587. if(last_str)
  1588. slapi_ch_free_string(&last_str);
  1589. return rc;
  1590. }
  1591. /*
  1592. * memberof_add_smod_list()
  1593. *
  1594. * Add group DN to the memberof attribute of the list of targets
  1595. *
  1596. */
  1597. int memberof_add_smod_list(Slapi_PBlock *pb, MemberOfConfig *config,
  1598. Slapi_DN *group_sdn, Slapi_Mod *smod)
  1599. {
  1600. return memberof_mod_smod_list(pb, config, LDAP_MOD_ADD, group_sdn, smod);
  1601. }
  1602. /*
  1603. * memberof_del_smod_list()
  1604. *
  1605. * Remove group DN from the memberof attribute of the list of targets
  1606. *
  1607. */
  1608. int memberof_del_smod_list(Slapi_PBlock *pb, MemberOfConfig *config,
  1609. Slapi_DN *group_sdn, Slapi_Mod *smod)
  1610. {
  1611. return memberof_mod_smod_list(pb, config, LDAP_MOD_DELETE, group_sdn, smod);
  1612. }
  1613. /**
  1614. * Plugin identity mgmt
  1615. */
  1616. void memberof_set_plugin_id(void * plugin_id)
  1617. {
  1618. _PluginID=plugin_id;
  1619. }
  1620. void * memberof_get_plugin_id()
  1621. {
  1622. return _PluginID;
  1623. }
  1624. /*
  1625. * memberof_mod_attr_list()
  1626. *
  1627. * Perform mod for group DN to the memberof attribute of the list of targets
  1628. *
  1629. */
  1630. int memberof_mod_attr_list(Slapi_PBlock *pb, MemberOfConfig *config, int mod,
  1631. Slapi_DN *group_sdn, Slapi_Attr *attr)
  1632. {
  1633. return memberof_mod_attr_list_r(pb, config, mod, group_sdn, group_sdn,
  1634. attr, 0);
  1635. }
  1636. int memberof_mod_attr_list_r(Slapi_PBlock *pb, MemberOfConfig *config, int mod,
  1637. Slapi_DN *group_sdn, Slapi_DN *op_this_sdn,
  1638. Slapi_Attr *attr, memberofstringll *stack)
  1639. {
  1640. int rc = 0;
  1641. Slapi_Value *val = 0;
  1642. Slapi_Value *op_this_val = 0;
  1643. int last_size = 0;
  1644. char *last_str = 0;
  1645. int hint = slapi_attr_first_value(attr, &val);
  1646. Slapi_DN *sdn = slapi_sdn_new();
  1647. op_this_val = slapi_value_new_string(slapi_sdn_get_ndn(op_this_sdn));
  1648. slapi_value_set_flags(op_this_val, SLAPI_ATTR_FLAG_NORMALIZED_CIS);
  1649. while(val && rc == 0)
  1650. {
  1651. char *dn_str = 0;
  1652. struct berval *bv = 0;
  1653. /* We don't want to process a memberOf operation on ourselves. */
  1654. if(0 != memberof_compare(config, &val, &op_this_val))
  1655. {
  1656. bv = (struct berval *)slapi_value_get_berval(val);
  1657. if(last_size > bv->bv_len)
  1658. {
  1659. dn_str = last_str;
  1660. }
  1661. else
  1662. {
  1663. int the_size = (bv->bv_len * 2) + 1;
  1664. if(last_str)
  1665. slapi_ch_free_string(&last_str);
  1666. dn_str = (char*)slapi_ch_malloc(the_size);
  1667. last_str = dn_str;
  1668. last_size = the_size;
  1669. }
  1670. memset(dn_str, 0, last_size);
  1671. strncpy(dn_str, bv->bv_val, (size_t)bv->bv_len);
  1672. /* If we're doing a replace (as we would in the MODRDN case), we need
  1673. * to specify the new group DN value */
  1674. slapi_sdn_set_normdn_byref(sdn, dn_str); /* dn_str is normalized */
  1675. if(mod == LDAP_MOD_REPLACE)
  1676. {
  1677. rc = memberof_modop_one_replace_r(pb, config, mod, group_sdn,
  1678. op_this_sdn, group_sdn,
  1679. sdn, stack);
  1680. }
  1681. else
  1682. {
  1683. rc = memberof_modop_one_r(pb, config, mod, group_sdn,
  1684. op_this_sdn, sdn, stack);
  1685. }
  1686. }
  1687. hint = slapi_attr_next_value(attr, hint, &val);
  1688. }
  1689. slapi_value_free(&op_this_val);
  1690. slapi_sdn_free(&sdn);
  1691. if(last_str)
  1692. slapi_ch_free_string(&last_str);
  1693. return rc;
  1694. }
  1695. /*
  1696. * memberof_add_attr_list()
  1697. *
  1698. * Add group DN to the memberof attribute of the list of targets
  1699. *
  1700. */
  1701. int memberof_add_attr_list(Slapi_PBlock *pb, MemberOfConfig *config,
  1702. Slapi_DN *group_sdn, Slapi_Attr *attr)
  1703. {
  1704. return memberof_mod_attr_list(pb, config, LDAP_MOD_ADD, group_sdn, attr);
  1705. }
  1706. /*
  1707. * memberof_del_attr_list()
  1708. *
  1709. * Remove group DN from the memberof attribute of the list of targets
  1710. *
  1711. */
  1712. int memberof_del_attr_list(Slapi_PBlock *pb, MemberOfConfig *config,
  1713. Slapi_DN *group_sdn, Slapi_Attr *attr)
  1714. {
  1715. return memberof_mod_attr_list(pb, config, LDAP_MOD_DELETE, group_sdn, attr);
  1716. }
  1717. /*
  1718. * memberof_moddn_attr_list()
  1719. *
  1720. * Perform mod for group DN to the memberof attribute of the list of targets
  1721. *
  1722. */
  1723. int memberof_moddn_attr_list(Slapi_PBlock *pb, MemberOfConfig *config,
  1724. Slapi_DN *pre_sdn, Slapi_DN *post_sdn, Slapi_Attr *attr)
  1725. {
  1726. int rc = 0;
  1727. Slapi_Value *val = 0;
  1728. int last_size = 0;
  1729. char *last_str = 0;
  1730. int hint = slapi_attr_first_value(attr, &val);
  1731. Slapi_DN *sdn = slapi_sdn_new();
  1732. while(val)
  1733. {
  1734. char *dn_str = 0;
  1735. struct berval *bv = (struct berval *)slapi_value_get_berval(val);
  1736. if(last_size > bv->bv_len)
  1737. {
  1738. dn_str = last_str;
  1739. }
  1740. else
  1741. {
  1742. int the_size = (bv->bv_len * 2) + 1;
  1743. if(last_str)
  1744. slapi_ch_free_string(&last_str);
  1745. dn_str = (char*)slapi_ch_malloc(the_size);
  1746. last_str = dn_str;
  1747. last_size = the_size;
  1748. }
  1749. memset(dn_str, 0, last_size);
  1750. strncpy(dn_str, bv->bv_val, (size_t)bv->bv_len);
  1751. slapi_sdn_set_normdn_byref(sdn, dn_str); /* dn_str is normalized */
  1752. memberof_modop_one_replace_r(pb, config, LDAP_MOD_REPLACE,
  1753. post_sdn, pre_sdn, post_sdn, sdn, 0);
  1754. hint = slapi_attr_next_value(attr, hint, &val);
  1755. }
  1756. slapi_sdn_free(&sdn);
  1757. if(last_str)
  1758. slapi_ch_free_string(&last_str);
  1759. return rc;
  1760. }
  1761. /* memberof_get_groups()
  1762. *
  1763. * Gets a list of all groups that an entry is a member of.
  1764. * This is done by looking only at member attribute values.
  1765. * A Slapi_ValueSet* is returned. It is up to the caller to
  1766. * free it.
  1767. */
  1768. Slapi_ValueSet *
  1769. memberof_get_groups(MemberOfConfig *config, Slapi_DN *member_sdn)
  1770. {
  1771. Slapi_ValueSet *groupvals = slapi_valueset_new();
  1772. Slapi_ValueSet *group_norm_vals = slapi_valueset_new();
  1773. Slapi_Value *memberdn_val =
  1774. slapi_value_new_string(slapi_sdn_get_ndn(member_sdn));
  1775. slapi_value_set_flags(memberdn_val, SLAPI_ATTR_FLAG_NORMALIZED_CIS);
  1776. memberof_get_groups_data data = {config, memberdn_val, &groupvals, &group_norm_vals};
  1777. memberof_get_groups_r(config, member_sdn, &data);
  1778. slapi_value_free(&memberdn_val);
  1779. slapi_valueset_free(group_norm_vals);
  1780. return groupvals;
  1781. }
  1782. int
  1783. memberof_get_groups_r(MemberOfConfig *config, Slapi_DN *member_sdn,
  1784. memberof_get_groups_data *data)
  1785. {
  1786. /* Search for any grouping attributes that point to memberdn.
  1787. * For each match, add it to the list, recurse and do same search */
  1788. return memberof_call_foreach_dn(NULL, member_sdn, config, config->groupattrs,
  1789. memberof_get_groups_callback, data);
  1790. }
  1791. /* memberof_get_groups_callback()
  1792. *
  1793. * Callback to perform work of memberof_get_groups()
  1794. */
  1795. int memberof_get_groups_callback(Slapi_Entry *e, void *callback_data)
  1796. {
  1797. Slapi_DN *group_sdn = slapi_entry_get_sdn(e);
  1798. char *group_ndn = slapi_entry_get_ndn(e);
  1799. char *group_dn = slapi_entry_get_dn(e);
  1800. Slapi_Value *group_ndn_val = 0;
  1801. Slapi_Value *group_dn_val = 0;
  1802. Slapi_ValueSet *groupvals = *((memberof_get_groups_data*)callback_data)->groupvals;
  1803. Slapi_ValueSet *group_norm_vals = *((memberof_get_groups_data*)callback_data)->group_norm_vals;
  1804. MemberOfConfig *config = ((memberof_get_groups_data*)callback_data)->config;
  1805. int rc = 0;
  1806. if(slapi_is_shutting_down()){
  1807. rc = -1;
  1808. goto bail;
  1809. }
  1810. if (!groupvals || !group_norm_vals)
  1811. {
  1812. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  1813. "memberof_get_groups_callback: NULL groupvals or group_norm_vals\n");
  1814. rc = -1;
  1815. goto bail;
  1816. }
  1817. /* get the DN of the group */
  1818. group_ndn_val = slapi_value_new_string(group_ndn);
  1819. /* group_dn is case-normalized */
  1820. slapi_value_set_flags(group_ndn_val, SLAPI_ATTR_FLAG_NORMALIZED_CIS);
  1821. /* check if e is the same as our original member entry */
  1822. if (0 == memberof_compare(((memberof_get_groups_data*)callback_data)->config,
  1823. &((memberof_get_groups_data*)callback_data)->memberdn_val, &group_ndn_val))
  1824. {
  1825. /* A recursive group caused us to find our original
  1826. * entry we passed to memberof_get_groups(). We just
  1827. * skip processing this entry. */
  1828. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  1829. "memberof_get_groups_callback: group recursion"
  1830. " detected in %s\n" ,group_ndn);
  1831. slapi_value_free(&group_ndn_val);
  1832. goto bail;
  1833. }
  1834. /* Have we been here before? Note that we don't loop through all of the group_slapiattrs
  1835. * in config. We only need this attribute for it's syntax so the comparison can be
  1836. * performed. Since all of the grouping attributes are validated to use the Dinstinguished
  1837. * Name syntax, we can safely just use the first group_slapiattr. */
  1838. if (slapi_valueset_find(
  1839. ((memberof_get_groups_data*)callback_data)->config->group_slapiattrs[0], group_norm_vals, group_ndn_val))
  1840. {
  1841. /* we either hit a recursive grouping, or an entry is
  1842. * a member of a group through multiple paths. Either
  1843. * way, we can just skip processing this entry since we've
  1844. * already gone through this part of the grouping hierarchy. */
  1845. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  1846. "memberof_get_groups_callback: possible group recursion"
  1847. " detected in %s\n" ,group_ndn);
  1848. slapi_value_free(&group_ndn_val);
  1849. goto bail;
  1850. }
  1851. /* if the group does not belong to an excluded subtree, adds it to the valueset */
  1852. if (memberof_entry_in_scope(config, group_sdn)) {
  1853. /* Push group_dn_val into the valueset. This memory is now owned
  1854. * by the valueset. */
  1855. group_dn_val = slapi_value_new_string(group_dn);
  1856. slapi_valueset_add_value_ext(groupvals, group_dn_val, SLAPI_VALUE_FLAG_PASSIN);
  1857. slapi_valueset_add_value_ext(group_norm_vals, group_ndn_val, SLAPI_VALUE_FLAG_PASSIN);
  1858. }
  1859. if(!config->skip_nested || config->fixup_task){
  1860. /* now recurse to find parent groups of e */
  1861. memberof_get_groups_r(((memberof_get_groups_data*)callback_data)->config,
  1862. group_sdn, callback_data);
  1863. }
  1864. bail:
  1865. return rc;
  1866. }
  1867. /* memberof_is_direct_member()
  1868. *
  1869. * tests for direct membership of memberdn in group groupdn
  1870. * returns non-zero when true, zero otherwise
  1871. */
  1872. int memberof_is_direct_member(MemberOfConfig *config, Slapi_Value *groupdn,
  1873. Slapi_Value *memberdn)
  1874. {
  1875. int rc = 0;
  1876. Slapi_DN *sdn = 0;
  1877. Slapi_Entry *group_e = 0;
  1878. Slapi_Attr *attr = 0;
  1879. int i = 0;
  1880. sdn = slapi_sdn_new_normdn_byref(slapi_value_get_string(groupdn));
  1881. slapi_search_internal_get_entry(sdn, config->groupattrs,
  1882. &group_e, memberof_get_plugin_id());
  1883. if(group_e)
  1884. {
  1885. /* See if memberdn is referred to by any of the group attributes. */
  1886. for (i = 0; config->groupattrs[i]; i++)
  1887. {
  1888. slapi_entry_attr_find(group_e, config->groupattrs[i], &attr );
  1889. if(attr && (0 == slapi_attr_value_find(attr, slapi_value_get_berval(memberdn))))
  1890. {
  1891. rc = 1;
  1892. break;
  1893. }
  1894. }
  1895. slapi_entry_free(group_e);
  1896. }
  1897. slapi_sdn_free(&sdn);
  1898. return rc;
  1899. }
  1900. /* memberof_is_grouping_attr()
  1901. *
  1902. * Checks if a supplied attribute is one of the configured
  1903. * grouping attributes.
  1904. *
  1905. * Returns non-zero when true, zero otherwise.
  1906. */
  1907. static int memberof_is_grouping_attr(char *type, MemberOfConfig *config)
  1908. {
  1909. int match = 0;
  1910. int i = 0;
  1911. for (i = 0; config && config->groupattrs[i]; i++)
  1912. {
  1913. match = slapi_attr_types_equivalent(type, config->groupattrs[i]);
  1914. if (match)
  1915. {
  1916. /* If we found a match, we're done. */
  1917. break;
  1918. }
  1919. }
  1920. return match;
  1921. }
  1922. /* memberof_test_membership()
  1923. *
  1924. * Finds all entries who are a "memberOf" the group
  1925. * represented by "group_dn". For each matching entry, we
  1926. * call memberof_test_membership_callback().
  1927. *
  1928. * for each attribute in the memberof attribute
  1929. * determine if the entry is still a member.
  1930. *
  1931. * test each for direct membership
  1932. * move groups entry is memberof to member group
  1933. * test remaining groups for membership in member groups
  1934. * iterate until a pass fails to move a group over to member groups
  1935. * remaining groups should be deleted
  1936. */
  1937. int
  1938. memberof_test_membership(Slapi_PBlock *pb, MemberOfConfig *config,
  1939. Slapi_DN *group_sdn)
  1940. {
  1941. char *attrs[2] = {config->memberof_attr, 0};
  1942. return memberof_call_foreach_dn(pb, group_sdn, config, attrs,
  1943. memberof_test_membership_callback, config);
  1944. }
  1945. /*
  1946. * memberof_test_membership_callback()
  1947. *
  1948. * A callback function to do the work of memberof_test_membership().
  1949. * Note that this not only tests membership, but updates the memberOf
  1950. * attributes in the entry to be correct.
  1951. */
  1952. int memberof_test_membership_callback(Slapi_Entry *e, void *callback_data)
  1953. {
  1954. int rc = 0;
  1955. Slapi_Attr *attr = 0;
  1956. int total = 0;
  1957. Slapi_Value **member_array = 0;
  1958. Slapi_Value **candidate_array = 0;
  1959. Slapi_Value *entry_dn = 0;
  1960. Slapi_DN *entry_sdn = 0;
  1961. MemberOfConfig *config = (MemberOfConfig *)callback_data;
  1962. Slapi_DN *sdn = slapi_sdn_new();
  1963. entry_sdn = slapi_entry_get_sdn(e);
  1964. entry_dn = slapi_value_new_string(slapi_entry_get_ndn(e));
  1965. if(entry_dn == NULL){
  1966. goto bail;
  1967. }
  1968. slapi_value_set_flags(entry_dn, SLAPI_ATTR_FLAG_NORMALIZED_CIS);
  1969. /* divide groups into member and non-member lists */
  1970. slapi_entry_attr_find(e, config->memberof_attr, &attr );
  1971. if(attr)
  1972. {
  1973. slapi_attr_get_numvalues( attr, &total);
  1974. if(total)
  1975. {
  1976. Slapi_Value *val = 0;
  1977. int hint = 0;
  1978. int c_index = 0;
  1979. int m_index = 0;
  1980. int member_found = 1;
  1981. int outer_index = 0;
  1982. candidate_array =
  1983. (Slapi_Value**)
  1984. slapi_ch_malloc(sizeof(Slapi_Value*)*total);
  1985. memset(candidate_array, 0, sizeof(Slapi_Value*)*total);
  1986. member_array =
  1987. (Slapi_Value**)
  1988. slapi_ch_malloc(sizeof(Slapi_Value*)*total);
  1989. memset(member_array, 0, sizeof(Slapi_Value*)*total);
  1990. hint = slapi_attr_first_value(attr, &val);
  1991. while(val)
  1992. {
  1993. /* test for direct membership */
  1994. if(memberof_is_direct_member(config, val, entry_dn))
  1995. {
  1996. /* it is a member */
  1997. member_array[m_index] = val;
  1998. m_index++;
  1999. }
  2000. else
  2001. {
  2002. /* not a member, still a candidate */
  2003. candidate_array[c_index] = val;
  2004. c_index++;
  2005. }
  2006. hint = slapi_attr_next_value(attr, hint, &val);
  2007. }
  2008. /* now iterate over members testing for membership
  2009. in candidate groups and moving candidates to members
  2010. when successful, quit when a full iteration adds no
  2011. new members
  2012. */
  2013. while(member_found)
  2014. {
  2015. member_found = 0;
  2016. /* For each group that this entry is a verified member of, see if
  2017. * any of the candidate groups are members. If they are, add them
  2018. * to the list of verified groups that this entry is a member of.
  2019. */
  2020. while(outer_index < m_index)
  2021. {
  2022. int inner_index = 0;
  2023. while(inner_index < c_index)
  2024. {
  2025. /* Check for a special value in this position
  2026. * that indicates that the candidate was moved
  2027. * to the member array. */
  2028. if((void*)1 == candidate_array[inner_index])
  2029. {
  2030. /* was moved, skip */
  2031. inner_index++;
  2032. continue;
  2033. }
  2034. if(memberof_is_direct_member(
  2035. config,
  2036. candidate_array[inner_index],
  2037. member_array[outer_index]))
  2038. {
  2039. member_array[m_index] =
  2040. candidate_array
  2041. [inner_index];
  2042. m_index++;
  2043. candidate_array[inner_index] =
  2044. (void*)1;
  2045. member_found = 1;
  2046. }
  2047. inner_index++;
  2048. }
  2049. outer_index++;
  2050. }
  2051. }
  2052. /* here we are left only with values to delete
  2053. from the memberof attribute in the candidate list
  2054. */
  2055. outer_index = 0;
  2056. while(outer_index < c_index)
  2057. {
  2058. /* Check for a special value in this position
  2059. * that indicates that the candidate was moved
  2060. * to the member array. */
  2061. if((void*)1 == candidate_array[outer_index])
  2062. {
  2063. /* item moved, skip */
  2064. outer_index++;
  2065. continue;
  2066. }
  2067. slapi_sdn_set_normdn_byref(sdn,
  2068. slapi_value_get_string(candidate_array[outer_index]));
  2069. memberof_del_one(0, config, sdn, entry_sdn);
  2070. outer_index++;
  2071. }
  2072. {
  2073. /* crazyness follows:
  2074. * strict-aliasing doesn't like the required cast
  2075. * to void for slapi_ch_free so we are made to
  2076. * juggle to get a normal thing done
  2077. */
  2078. void *pmember_array = member_array;
  2079. void *pcandidate_array = candidate_array;
  2080. slapi_ch_free(&pcandidate_array);
  2081. slapi_ch_free(&pmember_array);
  2082. candidate_array = 0;
  2083. member_array = 0;
  2084. }
  2085. }
  2086. }
  2087. bail:
  2088. slapi_sdn_free(&sdn);
  2089. slapi_value_free(&entry_dn);
  2090. return rc;
  2091. }
  2092. /*
  2093. * memberof_replace_list()
  2094. *
  2095. * Perform replace the group DN list in the memberof attribute of the list of targets
  2096. *
  2097. */
  2098. int
  2099. memberof_replace_list(Slapi_PBlock *pb, MemberOfConfig *config,
  2100. Slapi_DN *group_sdn)
  2101. {
  2102. struct slapi_entry *pre_e = NULL;
  2103. struct slapi_entry *post_e = NULL;
  2104. Slapi_Attr *pre_attr = 0;
  2105. Slapi_Attr *post_attr = 0;
  2106. int rc = 0;
  2107. int i = 0;
  2108. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &pre_e );
  2109. slapi_pblock_get( pb, SLAPI_ENTRY_POST_OP, &post_e );
  2110. for (i = 0; config && config->groupattrs[i]; i++)
  2111. {
  2112. if(pre_e && post_e)
  2113. {
  2114. slapi_entry_attr_find( pre_e, config->groupattrs[i], &pre_attr );
  2115. slapi_entry_attr_find( post_e, config->groupattrs[i], &post_attr );
  2116. }
  2117. if(pre_attr || post_attr)
  2118. {
  2119. int pre_total = 0;
  2120. int post_total = 0;
  2121. Slapi_Value **pre_array = 0;
  2122. Slapi_Value **post_array = 0;
  2123. int pre_index = 0;
  2124. int post_index = 0;
  2125. Slapi_DN *sdn = slapi_sdn_new();
  2126. /* create arrays of values */
  2127. if(pre_attr)
  2128. {
  2129. slapi_attr_get_numvalues( pre_attr, &pre_total);
  2130. }
  2131. if(post_attr)
  2132. {
  2133. slapi_attr_get_numvalues( post_attr, &post_total);
  2134. }
  2135. /* Stash a plugin global pointer here and have memberof_qsort_compare
  2136. * use it. We have to do this because we use memberof_qsort_compare
  2137. * as the comparator function for qsort, which requires the function
  2138. * to only take two void* args. This is thread-safe since we only
  2139. * store and use the pointer while holding the memberOf operation
  2140. * lock. */
  2141. qsortConfig = config;
  2142. if(pre_total)
  2143. {
  2144. pre_array =
  2145. (Slapi_Value**)
  2146. slapi_ch_malloc(sizeof(Slapi_Value*)*pre_total);
  2147. memberof_load_array(pre_array, pre_attr);
  2148. qsort(
  2149. pre_array,
  2150. pre_total,
  2151. sizeof(Slapi_Value*),
  2152. memberof_qsort_compare);
  2153. }
  2154. if(post_total)
  2155. {
  2156. post_array =
  2157. (Slapi_Value**)
  2158. slapi_ch_malloc(sizeof(Slapi_Value*)*post_total);
  2159. memberof_load_array(post_array, post_attr);
  2160. qsort(
  2161. post_array,
  2162. post_total,
  2163. sizeof(Slapi_Value*),
  2164. memberof_qsort_compare);
  2165. }
  2166. qsortConfig = 0;
  2167. /* work through arrays, following these rules:
  2168. in pre, in post, do nothing
  2169. in pre, not in post, delete from entry
  2170. not in pre, in post, add to entry
  2171. */
  2172. while(rc == 0 && (pre_index < pre_total || post_index < post_total))
  2173. {
  2174. if(pre_index == pre_total)
  2175. {
  2176. /* add the rest of post */
  2177. slapi_sdn_set_normdn_byref(sdn,
  2178. slapi_value_get_string(post_array[post_index]));
  2179. rc = memberof_add_one(pb, config, group_sdn, sdn);
  2180. post_index++;
  2181. }
  2182. else if(post_index == post_total)
  2183. {
  2184. /* delete the rest of pre */
  2185. slapi_sdn_set_normdn_byref(sdn,
  2186. slapi_value_get_string(pre_array[pre_index]));
  2187. rc = memberof_del_one(pb, config, group_sdn, sdn);
  2188. pre_index++;
  2189. }
  2190. else
  2191. {
  2192. /* decide what to do */
  2193. int cmp = memberof_compare(
  2194. config,
  2195. &(pre_array[pre_index]),
  2196. &(post_array[post_index]));
  2197. if(cmp < 0)
  2198. {
  2199. /* delete pre array */
  2200. slapi_sdn_set_normdn_byref(sdn,
  2201. slapi_value_get_string(pre_array[pre_index]));
  2202. rc = memberof_del_one(pb, config, group_sdn, sdn);
  2203. pre_index++;
  2204. }
  2205. else if(cmp > 0)
  2206. {
  2207. /* add post array */
  2208. slapi_sdn_set_normdn_byref(sdn,
  2209. slapi_value_get_string(post_array[post_index]));
  2210. rc = memberof_add_one(pb, config, group_sdn, sdn);
  2211. post_index++;
  2212. }
  2213. else
  2214. {
  2215. /* do nothing, advance */
  2216. pre_index++;
  2217. post_index++;
  2218. }
  2219. }
  2220. }
  2221. slapi_sdn_free(&sdn);
  2222. slapi_ch_free((void **)&pre_array);
  2223. slapi_ch_free((void **)&post_array);
  2224. }
  2225. }
  2226. return rc;
  2227. }
  2228. /* memberof_load_array()
  2229. *
  2230. * put attribute values in array structure
  2231. */
  2232. void memberof_load_array(Slapi_Value **array, Slapi_Attr *attr)
  2233. {
  2234. Slapi_Value *val = 0;
  2235. int hint = slapi_attr_first_value(attr, &val);
  2236. while(val)
  2237. {
  2238. *array = val;
  2239. array++;
  2240. hint = slapi_attr_next_value(attr, hint, &val);
  2241. }
  2242. }
  2243. /* memberof_compare()
  2244. *
  2245. * compare two attr values
  2246. */
  2247. int memberof_compare(MemberOfConfig *config, const void *a, const void *b)
  2248. {
  2249. Slapi_Value *val1;
  2250. Slapi_Value *val2;
  2251. if(a == NULL && b != NULL){
  2252. return 1;
  2253. } else if(a != NULL && b == NULL){
  2254. return -1;
  2255. } else if(a == NULL && b == NULL){
  2256. return 0;
  2257. }
  2258. val1 = *((Slapi_Value **)a);
  2259. val2 = *((Slapi_Value **)b);
  2260. /* We only need to provide a Slapi_Attr here for it's syntax. We
  2261. * already validated all grouping attributes to use the Distinguished
  2262. * Name syntax, so we can safely just use the first attr. */
  2263. return slapi_attr_value_cmp_ext(config->group_slapiattrs[0], val1, val2);
  2264. }
  2265. /* memberof_qsort_compare()
  2266. *
  2267. * This is a version of memberof_compare that uses a plugin
  2268. * global copy of the config. We'd prefer to pass in a copy
  2269. * of config that is local to the running thread, but we can't
  2270. * do this since qsort is using us as a comparator function.
  2271. * We should only use this function when using qsort, and only
  2272. * when the memberOf lock is acquired.
  2273. */
  2274. int memberof_qsort_compare(const void *a, const void *b)
  2275. {
  2276. Slapi_Value *val1 = *((Slapi_Value **)a);
  2277. Slapi_Value *val2 = *((Slapi_Value **)b);
  2278. /* We only need to provide a Slapi_Attr here for it's syntax. We
  2279. * already validated all grouping attributes to use the Distinguished
  2280. * Name syntax, so we can safely just use the first attr. */
  2281. return slapi_attr_value_cmp_ext(qsortConfig->group_slapiattrs[0],
  2282. val1, val2);
  2283. }
  2284. /* betxn: This locking mechanism is necessary to guarantee the memberof
  2285. * consistency */
  2286. void memberof_lock()
  2287. {
  2288. if (usetxn) {
  2289. PR_EnterMonitor(memberof_operation_lock);
  2290. }
  2291. }
  2292. void memberof_unlock()
  2293. {
  2294. if (usetxn) {
  2295. PR_ExitMonitor(memberof_operation_lock);
  2296. }
  2297. }
  2298. typedef struct _task_data
  2299. {
  2300. char *dn;
  2301. char *bind_dn;
  2302. char *filter_str;
  2303. } task_data;
  2304. void memberof_fixup_task_thread(void *arg)
  2305. {
  2306. MemberOfConfig configCopy = {0, 0, 0, 0};
  2307. Slapi_Task *task = (Slapi_Task *)arg;
  2308. task_data *td = NULL;
  2309. int rc = 0;
  2310. Slapi_PBlock *fixup_pb = NULL;
  2311. if (!task) {
  2312. return; /* no task */
  2313. }
  2314. slapi_task_inc_refcount(task);
  2315. slapi_log_error(SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  2316. "memberof_fixup_task_thread --> refcount incremented.\n" );
  2317. /* Fetch our task data from the task */
  2318. td = (task_data *)slapi_task_get_data(task);
  2319. /* set bind DN in the thread data */
  2320. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  2321. slapi_task_begin(task, 1);
  2322. slapi_task_log_notice(task, "Memberof task starts (arg: %s) ...\n",
  2323. td->filter_str);
  2324. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  2325. "Memberof task starts (arg: %s) ...\n", td->filter_str);
  2326. /* We need to get the config lock first. Trying to get the
  2327. * config lock after we already hold the op lock can cause
  2328. * a deadlock. */
  2329. memberof_rlock_config();
  2330. /* copy config so it doesn't change out from under us */
  2331. memberof_copy_config(&configCopy, memberof_get_config());
  2332. memberof_unlock_config();
  2333. /* Mark this as a task operation */
  2334. configCopy.fixup_task = 1;
  2335. if (usetxn) {
  2336. Slapi_DN *sdn = slapi_sdn_new_dn_byref(td->dn);
  2337. Slapi_Backend *be = slapi_be_select(sdn);
  2338. slapi_sdn_free(&sdn);
  2339. if (be) {
  2340. fixup_pb = slapi_pblock_new();
  2341. slapi_pblock_set(fixup_pb, SLAPI_BACKEND, be);
  2342. rc = slapi_back_transaction_begin(fixup_pb);
  2343. if (rc) {
  2344. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  2345. "memberof_fixup_task_thread: failed to start transaction\n");
  2346. }
  2347. } else {
  2348. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  2349. "memberof_fixup_task_thread: failed to get be backend from %s\n",
  2350. td->dn);
  2351. }
  2352. }
  2353. /* get the memberOf operation lock */
  2354. memberof_lock();
  2355. /* do real work */
  2356. rc = memberof_fix_memberof(&configCopy, td->dn, td->filter_str);
  2357. /* release the memberOf operation lock */
  2358. memberof_unlock();
  2359. if (usetxn && fixup_pb) {
  2360. if (rc) { /* failes */
  2361. slapi_back_transaction_abort(fixup_pb);
  2362. } else {
  2363. slapi_back_transaction_commit(fixup_pb);
  2364. }
  2365. slapi_pblock_destroy(fixup_pb);
  2366. }
  2367. memberof_free_config(&configCopy);
  2368. slapi_task_log_notice(task, "Memberof task finished.");
  2369. slapi_task_log_status(task, "Memberof task finished.");
  2370. slapi_task_inc_progress(task);
  2371. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  2372. "Memberof task finished (arg: %s) ...\n", td->filter_str);
  2373. /* this will queue the destruction of the task */
  2374. slapi_task_finish(task, rc);
  2375. slapi_task_dec_refcount(task);
  2376. slapi_log_error(SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  2377. "memberof_fixup_task_thread <-- refcount decremented.\n");
  2378. }
  2379. /* extract a single value from the entry (as a string) -- if it's not in the
  2380. * entry, the default will be returned (which can be NULL).
  2381. * you do not need to free anything returned by this.
  2382. */
  2383. const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  2384. const char *default_val)
  2385. {
  2386. Slapi_Attr *attr;
  2387. Slapi_Value *val = NULL;
  2388. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  2389. return default_val;
  2390. slapi_attr_first_value(attr, &val);
  2391. return slapi_value_get_string(val);
  2392. }
  2393. int memberof_task_add(Slapi_PBlock *pb, Slapi_Entry *e,
  2394. Slapi_Entry *eAfter, int *returncode, char *returntext,
  2395. void *arg)
  2396. {
  2397. PRThread *thread = NULL;
  2398. int rv = SLAPI_DSE_CALLBACK_OK;
  2399. task_data *mytaskdata = NULL;
  2400. Slapi_Task *task = NULL;
  2401. char *bind_dn;
  2402. const char *filter;
  2403. const char *dn = 0;
  2404. *returncode = LDAP_SUCCESS;
  2405. /* get arg(s) */
  2406. if ((dn = fetch_attr(e, "basedn", 0)) == NULL)
  2407. {
  2408. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2409. rv = SLAPI_DSE_CALLBACK_ERROR;
  2410. goto out;
  2411. }
  2412. if ((filter = fetch_attr(e, "filter", "(|(objectclass=inetuser)(objectclass=inetadmin))")) == NULL)
  2413. {
  2414. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2415. rv = SLAPI_DSE_CALLBACK_ERROR;
  2416. goto out;
  2417. }
  2418. /* setup our task data */
  2419. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  2420. mytaskdata = (task_data*)slapi_ch_malloc(sizeof(task_data));
  2421. if (mytaskdata == NULL)
  2422. {
  2423. *returncode = LDAP_OPERATIONS_ERROR;
  2424. rv = SLAPI_DSE_CALLBACK_ERROR;
  2425. goto out;
  2426. }
  2427. mytaskdata->dn = slapi_ch_strdup(dn);
  2428. mytaskdata->filter_str = slapi_ch_strdup(filter);
  2429. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  2430. /* allocate new task now */
  2431. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  2432. /* register our destructor for cleaning up our private data */
  2433. slapi_task_set_destructor_fn(task, memberof_task_destructor);
  2434. /* Stash a pointer to our data in the task */
  2435. slapi_task_set_data(task, mytaskdata);
  2436. /* start the sample task as a separate thread */
  2437. thread = PR_CreateThread(PR_USER_THREAD, memberof_fixup_task_thread,
  2438. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2439. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2440. if (thread == NULL)
  2441. {
  2442. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  2443. "unable to create task thread!\n");
  2444. *returncode = LDAP_OPERATIONS_ERROR;
  2445. slapi_task_finish(task, *returncode);
  2446. rv = SLAPI_DSE_CALLBACK_ERROR;
  2447. } else {
  2448. rv = SLAPI_DSE_CALLBACK_OK;
  2449. }
  2450. out:
  2451. return rv;
  2452. }
  2453. void
  2454. memberof_task_destructor(Slapi_Task *task)
  2455. {
  2456. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  2457. "memberof_task_destructor -->\n" );
  2458. if (task) {
  2459. task_data *mydata = (task_data *)slapi_task_get_data(task);
  2460. while (slapi_task_get_refcount(task) > 0) {
  2461. /* Yield to wait for the fixup task finishes. */
  2462. DS_Sleep (PR_MillisecondsToInterval(100));
  2463. }
  2464. if (mydata) {
  2465. slapi_ch_free_string(&mydata->dn);
  2466. slapi_ch_free_string(&mydata->bind_dn);
  2467. slapi_ch_free_string(&mydata->filter_str);
  2468. /* Need to cast to avoid a compiler warning */
  2469. slapi_ch_free((void **)&mydata);
  2470. }
  2471. }
  2472. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  2473. "memberof_task_destructor <--\n" );
  2474. }
  2475. int memberof_fix_memberof(MemberOfConfig *config, char *dn, char *filter_str)
  2476. {
  2477. int rc = 0;
  2478. Slapi_PBlock *search_pb = slapi_pblock_new();
  2479. slapi_search_internal_set_pb(search_pb, dn,
  2480. LDAP_SCOPE_SUBTREE, filter_str, 0, 0,
  2481. 0, 0,
  2482. memberof_get_plugin_id(),
  2483. 0);
  2484. rc = slapi_search_internal_callback_pb(search_pb,
  2485. config,
  2486. 0, memberof_fix_memberof_callback,
  2487. 0);
  2488. slapi_pblock_destroy(search_pb);
  2489. return rc;
  2490. }
  2491. /* memberof_fix_memberof_callback()
  2492. * Add initial and/or fix up broken group list in entry
  2493. *
  2494. * 1. Remove all present memberOf values
  2495. * 2. Add direct group membership memberOf values
  2496. * 3. Add indirect group membership memberOf values
  2497. */
  2498. int memberof_fix_memberof_callback(Slapi_Entry *e, void *callback_data)
  2499. {
  2500. int rc = 0;
  2501. Slapi_DN *sdn = slapi_entry_get_sdn(e);
  2502. MemberOfConfig *config = (MemberOfConfig *)callback_data;
  2503. memberof_del_dn_data del_data = {0, config->memberof_attr};
  2504. Slapi_ValueSet *groups = 0;
  2505. /*
  2506. * If the server is ordered to shutdown, stop the fixup and return an error.
  2507. */
  2508. if (slapi_is_shutting_down()) {
  2509. rc = -1;
  2510. goto bail;
  2511. }
  2512. /* get a list of all of the groups this user belongs to */
  2513. groups = memberof_get_groups(config, sdn);
  2514. /* If we found some groups, replace the existing memberOf attribute
  2515. * with the found values. */
  2516. if (groups && slapi_valueset_count(groups))
  2517. {
  2518. Slapi_Value *val = 0;
  2519. Slapi_Mod *smod;
  2520. LDAPMod **mods = (LDAPMod **) slapi_ch_malloc(2 * sizeof(LDAPMod *));
  2521. int hint = 0;
  2522. smod = slapi_mod_new();
  2523. slapi_mod_init(smod, 0);
  2524. slapi_mod_set_operation(smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  2525. slapi_mod_set_type(smod, config->memberof_attr);
  2526. /* Loop through all of our values and add them to smod */
  2527. hint = slapi_valueset_first_value(groups, &val);
  2528. while (val)
  2529. {
  2530. /* this makes a copy of the berval */
  2531. slapi_mod_add_value(smod, slapi_value_get_berval(val));
  2532. hint = slapi_valueset_next_value(groups, hint, &val);
  2533. }
  2534. mods[0] = slapi_mod_get_ldapmod_passout(smod);
  2535. mods[1] = 0;
  2536. rc = memberof_add_memberof_attr(mods, slapi_sdn_get_dn(sdn), config->auto_add_oc);
  2537. ldap_mods_free(mods, 1);
  2538. slapi_mod_free(&smod);
  2539. } else {
  2540. /* No groups were found, so remove the memberOf attribute
  2541. * from this entry. */
  2542. memberof_del_dn_type_callback(e, &del_data);
  2543. }
  2544. slapi_valueset_free(groups);
  2545. bail:
  2546. return rc;
  2547. }
  2548. /*
  2549. * Add the "memberof" attribute to the entry. If we get an objectclass violation,
  2550. * check if we are auto adding an objectclass. IF so, add the oc, and try the
  2551. * operation one more time.
  2552. */
  2553. static int
  2554. memberof_add_memberof_attr(LDAPMod **mods, const char *dn, char *add_oc)
  2555. {
  2556. Slapi_PBlock *mod_pb = NULL;
  2557. int added_oc = 0;
  2558. int rc = 0;
  2559. while(1){
  2560. mod_pb = slapi_pblock_new();
  2561. slapi_modify_internal_set_pb(
  2562. mod_pb, dn, mods, 0, 0,
  2563. memberof_get_plugin_id(), 0);
  2564. slapi_modify_internal_pb(mod_pb);
  2565. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2566. if (rc == LDAP_OBJECT_CLASS_VIOLATION){
  2567. if (!add_oc || added_oc){
  2568. /*
  2569. * We aren't auto adding an objectclass, or we already
  2570. * added the objectclass, and we are still failing.
  2571. */
  2572. break;
  2573. }
  2574. if(memberof_add_objectclass(add_oc, dn)){
  2575. /* Failed to add objectclass */
  2576. break;
  2577. }
  2578. added_oc = 1;
  2579. slapi_pblock_destroy(mod_pb);
  2580. } else if (rc){
  2581. /* Some other fatal error */
  2582. break;
  2583. } else {
  2584. /* success */
  2585. break;
  2586. }
  2587. }
  2588. slapi_pblock_destroy(mod_pb);
  2589. return rc;
  2590. }
  2591. /*
  2592. * Add the "auto add" objectclass to an entry
  2593. */
  2594. static int
  2595. memberof_add_objectclass(char *auto_add_oc, const char *dn)
  2596. {
  2597. Slapi_PBlock *mod_pb = NULL;
  2598. LDAPMod mod;
  2599. LDAPMod *mods[2];
  2600. char *val[2];
  2601. int rc = 0;
  2602. mod_pb = slapi_pblock_new();
  2603. mods[0] = &mod;
  2604. mods[1] = 0;
  2605. val[0] = auto_add_oc;
  2606. val[1] = 0;
  2607. mod.mod_op = LDAP_MOD_ADD;
  2608. mod.mod_type = "objectclass";
  2609. mod.mod_values = val;
  2610. slapi_modify_internal_set_pb(
  2611. mod_pb, dn, mods, 0, 0,
  2612. memberof_get_plugin_id(), 0);
  2613. slapi_modify_internal_pb(mod_pb);
  2614. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2615. if (rc){
  2616. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  2617. "Failed to add objectclass (%s) to entry (%s)\n",
  2618. auto_add_oc, dn);
  2619. }
  2620. slapi_pblock_destroy(mod_pb);
  2621. return rc;
  2622. }