memberof.c 69 KB

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