memberof.c 60 KB

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