memberof.c 74 KB

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