memberof.c 80 KB

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