memberof.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  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. /* determine if this is a group op or single entry */
  832. op_to_sdn = slapi_sdn_new_dn_byref(op_to);
  833. slapi_search_internal_get_entry( op_to_sdn, config->groupattrs,
  834. &e, memberof_get_plugin_id());
  835. if(!e)
  836. {
  837. /* In the case of a delete, we need to worry about the
  838. * missing entry being a nested group. There's a small
  839. * window where another thread may have deleted a nested
  840. * group that our group_dn entry refers to. This has the
  841. * potential of us missing some indirect member entries
  842. * that need to be updated. */
  843. if(LDAP_MOD_DELETE == mod_op)
  844. {
  845. Slapi_PBlock *search_pb = slapi_pblock_new();
  846. Slapi_DN *base_sdn = 0;
  847. Slapi_Backend *be = 0;
  848. char *filter_str = 0;
  849. int n_entries = 0;
  850. /* We can't tell for sure if the op_to entry is a
  851. * user or a group since the entry doesn't exist
  852. * anymore. We can safely ignore the missing entry
  853. * if no other entries have a memberOf attribute that
  854. * points to the missing entry. */
  855. be = slapi_be_select(op_to_sdn);
  856. if(be)
  857. {
  858. base_sdn = (Slapi_DN*)slapi_be_getsuffix(be,0);
  859. }
  860. if(base_sdn)
  861. {
  862. filter_str = slapi_ch_smprintf("(%s=%s)",
  863. config->memberof_attr, op_to);
  864. }
  865. if(filter_str)
  866. {
  867. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_dn(base_sdn),
  868. LDAP_SCOPE_SUBTREE, filter_str, 0, 0, 0, 0,
  869. memberof_get_plugin_id(), 0);
  870. if (slapi_search_internal_pb(search_pb))
  871. {
  872. /* get result and log an error */
  873. int res = 0;
  874. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  875. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  876. "memberof_modop_one_replace_r: error searching for members: "
  877. "%d", res);
  878. } else {
  879. slapi_pblock_get(search_pb, SLAPI_NENTRIES, &n_entries);
  880. if(n_entries > 0)
  881. {
  882. /* We want to fixup the membership for the
  883. * entries that referred to the missing group
  884. * entry. This will fix the references to
  885. * the missing group as well as the group
  886. * represented by op_this. */
  887. memberof_test_membership(pb, config, op_to);
  888. }
  889. }
  890. slapi_free_search_results_internal(search_pb);
  891. slapi_ch_free_string(&filter_str);
  892. }
  893. slapi_pblock_destroy(search_pb);
  894. }
  895. goto bail;
  896. }
  897. if(LDAP_MOD_DELETE == mod_op)
  898. {
  899. op_str = "DELETE";
  900. }
  901. else if(LDAP_MOD_ADD == mod_op)
  902. {
  903. op_str = "ADD";
  904. }
  905. else if(LDAP_MOD_REPLACE == mod_op)
  906. {
  907. op_str = "REPLACE";
  908. }
  909. else
  910. {
  911. op_str = "UNKNOWN";
  912. }
  913. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  914. "memberof_modop_one_replace_r: %s %s in %s\n"
  915. ,op_str, op_this, op_to);
  916. if(config && config->group_filter && !slapi_filter_test_simple(e, config->group_filter))
  917. {
  918. /* group */
  919. Slapi_Value *ll_dn_val = 0;
  920. Slapi_Attr *members = 0;
  921. int i = 0;
  922. ll = stack;
  923. /* have we been here before? */
  924. while(ll)
  925. {
  926. ll_dn_val = slapi_value_new_string(ll->dn);
  927. if(0 == memberof_compare(config, &ll_dn_val, &to_dn_val))
  928. {
  929. slapi_value_free(&ll_dn_val);
  930. /* someone set up infinitely
  931. recursive groups - bail out */
  932. slapi_log_error( SLAPI_LOG_PLUGIN,
  933. MEMBEROF_PLUGIN_SUBSYSTEM,
  934. "memberof_modop_one_replace_r: group recursion"
  935. " detected in %s\n"
  936. ,op_to);
  937. goto bail;
  938. }
  939. slapi_value_free(&ll_dn_val);
  940. ll = ll->next;
  941. }
  942. /* do op on group */
  943. slapi_log_error( SLAPI_LOG_PLUGIN,
  944. MEMBEROF_PLUGIN_SUBSYSTEM,
  945. "memberof_modop_one_replace_r: descending into group %s\n",
  946. op_to);
  947. /* Add the nested group's DN to the stack so we can detect loops later. */
  948. ll = (memberofstringll*)slapi_ch_malloc(sizeof(memberofstringll));
  949. ll->dn = op_to;
  950. ll->next = stack;
  951. /* Go through each grouping attribute one at a time. */
  952. for (i = 0; config->groupattrs[i]; i++)
  953. {
  954. slapi_entry_attr_find( e, config->groupattrs[i], &members );
  955. if(members)
  956. {
  957. memberof_mod_attr_list_r(pb, config, mod_op, group_dn, op_this, members, ll);
  958. }
  959. }
  960. {
  961. /* crazyness follows:
  962. * strict-aliasing doesn't like the required cast
  963. * to void for slapi_ch_free so we are made to
  964. * juggle to get a normal thing done
  965. */
  966. void *pll = ll;
  967. slapi_ch_free(&pll);
  968. ll = 0;
  969. }
  970. }
  971. /* continue with operation */
  972. {
  973. /* We want to avoid listing a group as a memberOf itself
  974. * in case someone set up a circular grouping.
  975. */
  976. if (0 == memberof_compare(config, &this_dn_val, &to_dn_val))
  977. {
  978. const char *strval = "NULL";
  979. if (this_dn_val) {
  980. strval = slapi_value_get_string(this_dn_val);
  981. }
  982. slapi_log_error( SLAPI_LOG_PLUGIN,
  983. MEMBEROF_PLUGIN_SUBSYSTEM,
  984. "memberof_modop_one_replace_r: not processing memberOf "
  985. "operations on self entry: %s\n", strval);
  986. goto bail;
  987. }
  988. /* For add and del modify operations, we just regenerate the
  989. * memberOf attribute. */
  990. if(LDAP_MOD_DELETE == mod_op || LDAP_MOD_ADD == mod_op)
  991. {
  992. /* find parent groups and replace our member attr */
  993. memberof_fix_memberof_callback(e, config);
  994. } else {
  995. /* single entry - do mod */
  996. mod_pb = slapi_pblock_new();
  997. mods[0] = &mod;
  998. if(LDAP_MOD_REPLACE == mod_op)
  999. {
  1000. mods[1] = &replace_mod;
  1001. mods[2] = 0;
  1002. }
  1003. else
  1004. {
  1005. mods[1] = 0;
  1006. }
  1007. val[0] = op_this;
  1008. val[1] = 0;
  1009. mod.mod_op = LDAP_MOD_REPLACE == mod_op?LDAP_MOD_DELETE:mod_op;
  1010. mod.mod_type = config->memberof_attr;
  1011. mod.mod_values = val;
  1012. if(LDAP_MOD_REPLACE == mod_op)
  1013. {
  1014. replace_val[0] = replace_with;
  1015. replace_val[1] = 0;
  1016. replace_mod.mod_op = LDAP_MOD_ADD;
  1017. replace_mod.mod_type = config->memberof_attr;
  1018. replace_mod.mod_values = replace_val;
  1019. }
  1020. slapi_modify_internal_set_pb(
  1021. mod_pb, op_to,
  1022. mods, 0, 0,
  1023. memberof_get_plugin_id(), 0);
  1024. slapi_modify_internal_pb(mod_pb);
  1025. slapi_pblock_get(mod_pb,
  1026. SLAPI_PLUGIN_INTOP_RESULT,
  1027. &rc);
  1028. slapi_pblock_destroy(mod_pb);
  1029. }
  1030. }
  1031. bail:
  1032. slapi_sdn_free(&op_to_sdn);
  1033. slapi_value_free(&to_dn_val);
  1034. slapi_value_free(&this_dn_val);
  1035. slapi_entry_free(e);
  1036. return rc;
  1037. }
  1038. /*
  1039. * memberof_add_one()
  1040. *
  1041. * Add addthis DN to the memberof attribute of addto
  1042. *
  1043. */
  1044. int memberof_add_one(Slapi_PBlock *pb, MemberOfConfig *config, char *addthis, char *addto)
  1045. {
  1046. return memberof_modop_one(pb, config, LDAP_MOD_ADD, addthis, addto);
  1047. }
  1048. /*
  1049. * memberof_del_one()
  1050. *
  1051. * Delete delthis DN from the memberof attribute of delfrom
  1052. *
  1053. */
  1054. int memberof_del_one(Slapi_PBlock *pb, MemberOfConfig *config, char *delthis, char *delfrom)
  1055. {
  1056. return memberof_modop_one(pb, config, LDAP_MOD_DELETE, delthis, delfrom);
  1057. }
  1058. /*
  1059. * memberof_mod_smod_list()
  1060. *
  1061. * Perform mod for group DN to the memberof attribute of the list of targets
  1062. *
  1063. */
  1064. int memberof_mod_smod_list(Slapi_PBlock *pb, MemberOfConfig *config, int mod,
  1065. char *group_dn, Slapi_Mod *smod)
  1066. {
  1067. int rc = 0;
  1068. struct berval *bv = slapi_mod_get_first_value(smod);
  1069. int last_size = 0;
  1070. char *last_str = 0;
  1071. while(bv)
  1072. {
  1073. char *dn_str = 0;
  1074. if(last_size > bv->bv_len)
  1075. {
  1076. dn_str = last_str;
  1077. }
  1078. else
  1079. {
  1080. int the_size = (bv->bv_len * 2) + 1;
  1081. if(last_str)
  1082. slapi_ch_free_string(&last_str);
  1083. dn_str = (char*)slapi_ch_malloc(the_size);
  1084. last_str = dn_str;
  1085. last_size = the_size;
  1086. }
  1087. memset(dn_str, 0, last_size);
  1088. strncpy(dn_str, bv->bv_val, (size_t)bv->bv_len);
  1089. memberof_modop_one(pb, config, mod, group_dn, dn_str);
  1090. bv = slapi_mod_get_next_value(smod);
  1091. }
  1092. if(last_str)
  1093. slapi_ch_free_string(&last_str);
  1094. return rc;
  1095. }
  1096. /*
  1097. * memberof_add_smod_list()
  1098. *
  1099. * Add group DN to the memberof attribute of the list of targets
  1100. *
  1101. */
  1102. int memberof_add_smod_list(Slapi_PBlock *pb, MemberOfConfig *config,
  1103. char *groupdn, Slapi_Mod *smod)
  1104. {
  1105. return memberof_mod_smod_list(pb, config, LDAP_MOD_ADD, groupdn, smod);
  1106. }
  1107. /*
  1108. * memberof_del_smod_list()
  1109. *
  1110. * Remove group DN from the memberof attribute of the list of targets
  1111. *
  1112. */
  1113. int memberof_del_smod_list(Slapi_PBlock *pb, MemberOfConfig *config,
  1114. char *groupdn, Slapi_Mod *smod)
  1115. {
  1116. return memberof_mod_smod_list(pb, config, LDAP_MOD_DELETE, groupdn, smod);
  1117. }
  1118. /**
  1119. * Plugin identity mgmt
  1120. */
  1121. void memberof_set_plugin_id(void * plugin_id)
  1122. {
  1123. _PluginID=plugin_id;
  1124. }
  1125. void * memberof_get_plugin_id()
  1126. {
  1127. return _PluginID;
  1128. }
  1129. /*
  1130. * memberof_mod_attr_list()
  1131. *
  1132. * Perform mod for group DN to the memberof attribute of the list of targets
  1133. *
  1134. */
  1135. int memberof_mod_attr_list(Slapi_PBlock *pb, MemberOfConfig *config, int mod,
  1136. char *group_dn, Slapi_Attr *attr)
  1137. {
  1138. return memberof_mod_attr_list_r(pb, config, mod, group_dn, group_dn, attr, 0);
  1139. }
  1140. int memberof_mod_attr_list_r(Slapi_PBlock *pb, MemberOfConfig *config, int mod,
  1141. char *group_dn, char *op_this, Slapi_Attr *attr, memberofstringll *stack)
  1142. {
  1143. int rc = 0;
  1144. Slapi_Value *val = 0;
  1145. Slapi_Value *op_this_val = 0;
  1146. int last_size = 0;
  1147. char *last_str = 0;
  1148. int hint = slapi_attr_first_value(attr, &val);
  1149. op_this_val = slapi_value_new_string(op_this);
  1150. while(val)
  1151. {
  1152. char *dn_str = 0;
  1153. struct berval *bv = 0;
  1154. /* We don't want to process a memberOf operation on ourselves. */
  1155. if(0 != memberof_compare(config, &val, &op_this_val))
  1156. {
  1157. bv = (struct berval *)slapi_value_get_berval(val);
  1158. if(last_size > bv->bv_len)
  1159. {
  1160. dn_str = last_str;
  1161. }
  1162. else
  1163. {
  1164. int the_size = (bv->bv_len * 2) + 1;
  1165. if(last_str)
  1166. slapi_ch_free_string(&last_str);
  1167. dn_str = (char*)slapi_ch_malloc(the_size);
  1168. last_str = dn_str;
  1169. last_size = the_size;
  1170. }
  1171. memset(dn_str, 0, last_size);
  1172. strncpy(dn_str, bv->bv_val, (size_t)bv->bv_len);
  1173. /* If we're doing a replace (as we would in the MODRDN case), we need
  1174. * to specify the new group DN value */
  1175. if(mod == LDAP_MOD_REPLACE)
  1176. {
  1177. memberof_modop_one_replace_r(pb, config, mod, group_dn, op_this,
  1178. group_dn, dn_str, stack);
  1179. }
  1180. else
  1181. {
  1182. memberof_modop_one_r(pb, config, mod, group_dn, op_this, dn_str, stack);
  1183. }
  1184. }
  1185. hint = slapi_attr_next_value(attr, hint, &val);
  1186. }
  1187. slapi_value_free(&op_this_val);
  1188. if(last_str)
  1189. slapi_ch_free_string(&last_str);
  1190. return rc;
  1191. }
  1192. /*
  1193. * memberof_add_attr_list()
  1194. *
  1195. * Add group DN to the memberof attribute of the list of targets
  1196. *
  1197. */
  1198. int memberof_add_attr_list(Slapi_PBlock *pb, MemberOfConfig *config, char *groupdn,
  1199. Slapi_Attr *attr)
  1200. {
  1201. return memberof_mod_attr_list(pb, config, LDAP_MOD_ADD, groupdn, attr);
  1202. }
  1203. /*
  1204. * memberof_del_attr_list()
  1205. *
  1206. * Remove group DN from the memberof attribute of the list of targets
  1207. *
  1208. */
  1209. int memberof_del_attr_list(Slapi_PBlock *pb, MemberOfConfig *config, char *groupdn,
  1210. Slapi_Attr *attr)
  1211. {
  1212. return memberof_mod_attr_list(pb, config, LDAP_MOD_DELETE, groupdn, attr);
  1213. }
  1214. /*
  1215. * memberof_moddn_attr_list()
  1216. *
  1217. * Perform mod for group DN to the memberof attribute of the list of targets
  1218. *
  1219. */
  1220. int memberof_moddn_attr_list(Slapi_PBlock *pb, MemberOfConfig *config,
  1221. char *pre_dn, char *post_dn, Slapi_Attr *attr)
  1222. {
  1223. int rc = 0;
  1224. Slapi_Value *val = 0;
  1225. int last_size = 0;
  1226. char *last_str = 0;
  1227. int hint = slapi_attr_first_value(attr, &val);
  1228. while(val)
  1229. {
  1230. char *dn_str = 0;
  1231. struct berval *bv = (struct berval *)slapi_value_get_berval(val);
  1232. if(last_size > bv->bv_len)
  1233. {
  1234. dn_str = last_str;
  1235. }
  1236. else
  1237. {
  1238. int the_size = (bv->bv_len * 2) + 1;
  1239. if(last_str)
  1240. slapi_ch_free_string(&last_str);
  1241. dn_str = (char*)slapi_ch_malloc(the_size);
  1242. last_str = dn_str;
  1243. last_size = the_size;
  1244. }
  1245. memset(dn_str, 0, last_size);
  1246. strncpy(dn_str, bv->bv_val, (size_t)bv->bv_len);
  1247. memberof_modop_one_replace_r(pb, config, LDAP_MOD_REPLACE,
  1248. post_dn, pre_dn, post_dn, dn_str, 0);
  1249. hint = slapi_attr_next_value(attr, hint, &val);
  1250. }
  1251. if(last_str)
  1252. slapi_ch_free_string(&last_str);
  1253. return rc;
  1254. }
  1255. /* memberof_get_groups()
  1256. *
  1257. * Gets a list of all groups that an entry is a member of.
  1258. * This is done by looking only at member attribute values.
  1259. * A Slapi_ValueSet* is returned. It is up to the caller to
  1260. * free it.
  1261. */
  1262. Slapi_ValueSet *memberof_get_groups(MemberOfConfig *config, char *memberdn)
  1263. {
  1264. Slapi_Value *memberdn_val = slapi_value_new_string(memberdn);
  1265. Slapi_ValueSet *groupvals = slapi_valueset_new();
  1266. memberof_get_groups_data data = {config, memberdn_val, &groupvals};
  1267. memberof_get_groups_r(config, memberdn, &data);
  1268. slapi_value_free(&memberdn_val);
  1269. return groupvals;
  1270. }
  1271. int memberof_get_groups_r(MemberOfConfig *config, char *memberdn, memberof_get_groups_data *data)
  1272. {
  1273. /* Search for any grouping attributes that point to memberdn.
  1274. * For each match, add it to the list, recurse and do same search */
  1275. return memberof_call_foreach_dn(NULL, memberdn, config->groupattrs,
  1276. memberof_get_groups_callback, data);
  1277. }
  1278. /* memberof_get_groups_callback()
  1279. *
  1280. * Callback to perform work of memberof_get_groups()
  1281. */
  1282. int memberof_get_groups_callback(Slapi_Entry *e, void *callback_data)
  1283. {
  1284. char *group_dn = slapi_entry_get_dn(e);
  1285. Slapi_Value *group_dn_val = 0;
  1286. Slapi_ValueSet *groupvals = *((memberof_get_groups_data*)callback_data)->groupvals;
  1287. int rc = 0;
  1288. if (!groupvals)
  1289. {
  1290. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  1291. "memberof_get_groups_callback: NULL groupvals\n");
  1292. rc = -1;
  1293. goto bail;
  1294. }
  1295. /* get the DN of the group */
  1296. group_dn_val = slapi_value_new_string(group_dn);
  1297. /* check if e is the same as our original member entry */
  1298. if (0 == memberof_compare(((memberof_get_groups_data*)callback_data)->config,
  1299. &((memberof_get_groups_data*)callback_data)->memberdn_val, &group_dn_val))
  1300. {
  1301. /* A recursive group caused us to find our original
  1302. * entry we passed to memberof_get_groups(). We just
  1303. * skip processing this entry. */
  1304. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  1305. "memberof_get_groups_callback: group recursion"
  1306. " detected in %s\n" ,group_dn);
  1307. slapi_value_free(&group_dn_val);
  1308. goto bail;
  1309. }
  1310. /* Have we been here before? Note that we don't loop through all of the group_slapiattrs
  1311. * in config. We only need this attribute for it's syntax so the comparison can be
  1312. * performed. Since all of the grouping attributes are validated to use the Dinstinguished
  1313. * Name syntax, we can safely just use the first group_slapiattr. */
  1314. if (groupvals && slapi_valueset_find(
  1315. ((memberof_get_groups_data*)callback_data)->config->group_slapiattrs[0], groupvals, group_dn_val))
  1316. {
  1317. /* we either hit a recursive grouping, or an entry is
  1318. * a member of a group through multiple paths. Either
  1319. * way, we can just skip processing this entry since we've
  1320. * already gone through this part of the grouping hierarchy. */
  1321. slapi_log_error( SLAPI_LOG_PLUGIN, MEMBEROF_PLUGIN_SUBSYSTEM,
  1322. "memberof_get_groups_callback: possible group recursion"
  1323. " detected in %s\n" ,group_dn);
  1324. slapi_value_free(&group_dn_val);
  1325. goto bail;
  1326. }
  1327. /* Push group_dn_val into the valueset. This memory is now owned
  1328. * by the valueset. */
  1329. slapi_valueset_add_value_ext(groupvals, group_dn_val, SLAPI_VALUE_FLAG_PASSIN);
  1330. /* now recurse to find parent groups of e */
  1331. memberof_get_groups_r(((memberof_get_groups_data*)callback_data)->config,
  1332. group_dn, callback_data);
  1333. bail:
  1334. return rc;
  1335. }
  1336. /* memberof_is_direct_member()
  1337. *
  1338. * tests for direct membership of memberdn in group groupdn
  1339. * returns non-zero when true, zero otherwise
  1340. */
  1341. int memberof_is_direct_member(MemberOfConfig *config, Slapi_Value *groupdn,
  1342. Slapi_Value *memberdn)
  1343. {
  1344. int rc = 0;
  1345. Slapi_DN *sdn = 0;
  1346. Slapi_Entry *group_e = 0;
  1347. Slapi_Attr *attr = 0;
  1348. int i = 0;
  1349. sdn = slapi_sdn_new_dn_byref(slapi_value_get_string(groupdn));
  1350. slapi_search_internal_get_entry(sdn, config->groupattrs,
  1351. &group_e, memberof_get_plugin_id());
  1352. if(group_e)
  1353. {
  1354. /* See if memberdn is referred to by any of the group attributes. */
  1355. for (i = 0; config->groupattrs[i]; i++)
  1356. {
  1357. slapi_entry_attr_find(group_e, config->groupattrs[i], &attr );
  1358. if(attr && (0 == slapi_attr_value_find(attr, slapi_value_get_berval(memberdn))))
  1359. {
  1360. rc = 1;
  1361. break;
  1362. }
  1363. }
  1364. slapi_entry_free(group_e);
  1365. }
  1366. slapi_sdn_free(&sdn);
  1367. return rc;
  1368. }
  1369. /* memberof_is_grouping_attr()
  1370. *
  1371. * Checks if a supplied attribute is one of the configured
  1372. * grouping attributes.
  1373. *
  1374. * Returns non-zero when true, zero otherwise.
  1375. */
  1376. static int memberof_is_grouping_attr(char *type, MemberOfConfig *config)
  1377. {
  1378. int match = 0;
  1379. int i = 0;
  1380. for (i = 0; config && config->groupattrs[i]; i++)
  1381. {
  1382. match = slapi_attr_types_equivalent(type, config->groupattrs[i]);
  1383. if (match)
  1384. {
  1385. /* If we found a match, we're done. */
  1386. break;
  1387. }
  1388. }
  1389. return match;
  1390. }
  1391. /* memberof_test_membership()
  1392. *
  1393. * Finds all entries who are a "memberOf" the group
  1394. * represented by "group_dn". For each matching entry, we
  1395. * call memberof_test_membership_callback().
  1396. *
  1397. * for each attribute in the memberof attribute
  1398. * determine if the entry is still a member.
  1399. *
  1400. * test each for direct membership
  1401. * move groups entry is memberof to member group
  1402. * test remaining groups for membership in member groups
  1403. * iterate until a pass fails to move a group over to member groups
  1404. * remaining groups should be deleted
  1405. */
  1406. int memberof_test_membership(Slapi_PBlock *pb, MemberOfConfig *config, char *group_dn)
  1407. {
  1408. char *attrs[2] = {config->memberof_attr, 0};
  1409. return memberof_call_foreach_dn(pb, group_dn, attrs,
  1410. memberof_test_membership_callback , config);
  1411. }
  1412. /*
  1413. * memberof_test_membership_callback()
  1414. *
  1415. * A callback function to do the work of memberof_test_membership().
  1416. * Note that this not only tests membership, but updates the memberOf
  1417. * attributes in the entry to be correct.
  1418. */
  1419. int memberof_test_membership_callback(Slapi_Entry *e, void *callback_data)
  1420. {
  1421. int rc = 0;
  1422. Slapi_Attr *attr = 0;
  1423. int total = 0;
  1424. Slapi_Value **member_array = 0;
  1425. Slapi_Value **candidate_array = 0;
  1426. Slapi_Value *entry_dn = 0;
  1427. MemberOfConfig *config = (MemberOfConfig *)callback_data;
  1428. entry_dn = slapi_value_new_string(slapi_entry_get_dn(e));
  1429. if(0 == entry_dn)
  1430. {
  1431. goto bail;
  1432. }
  1433. /* divide groups into member and non-member lists */
  1434. slapi_entry_attr_find(e, config->memberof_attr, &attr );
  1435. if(attr)
  1436. {
  1437. slapi_attr_get_numvalues( attr, &total);
  1438. if(total)
  1439. {
  1440. Slapi_Value *val = 0;
  1441. int hint = 0;
  1442. int c_index = 0;
  1443. int m_index = 0;
  1444. int member_found = 1;
  1445. int outer_index = 0;
  1446. candidate_array =
  1447. (Slapi_Value**)
  1448. slapi_ch_malloc(sizeof(Slapi_Value*)*total);
  1449. memset(candidate_array, 0, sizeof(Slapi_Value*)*total);
  1450. member_array =
  1451. (Slapi_Value**)
  1452. slapi_ch_malloc(sizeof(Slapi_Value*)*total);
  1453. memset(member_array, 0, sizeof(Slapi_Value*)*total);
  1454. hint = slapi_attr_first_value(attr, &val);
  1455. while(val)
  1456. {
  1457. /* test for direct membership */
  1458. if(memberof_is_direct_member(config, val, entry_dn))
  1459. {
  1460. /* it is a member */
  1461. member_array[m_index] = val;
  1462. m_index++;
  1463. }
  1464. else
  1465. {
  1466. /* not a member, still a candidate */
  1467. candidate_array[c_index] = val;
  1468. c_index++;
  1469. }
  1470. hint = slapi_attr_next_value(attr, hint, &val);
  1471. }
  1472. /* now iterate over members testing for membership
  1473. in candidate groups and moving candidates to members
  1474. when successful, quit when a full iteration adds no
  1475. new members
  1476. */
  1477. while(member_found)
  1478. {
  1479. member_found = 0;
  1480. /* For each group that this entry is a verified member of, see if
  1481. * any of the candidate groups are members. If they are, add them
  1482. * to the list of verified groups that this entry is a member of.
  1483. */
  1484. while(outer_index < m_index)
  1485. {
  1486. int inner_index = 0;
  1487. while(inner_index < c_index)
  1488. {
  1489. /* Check for a special value in this position
  1490. * that indicates that the candidate was moved
  1491. * to the member array. */
  1492. if((void*)1 ==
  1493. candidate_array[inner_index])
  1494. {
  1495. /* was moved, skip */
  1496. inner_index++;
  1497. continue;
  1498. }
  1499. if(memberof_is_direct_member(
  1500. config,
  1501. candidate_array[inner_index],
  1502. member_array[outer_index]))
  1503. {
  1504. member_array[m_index] =
  1505. candidate_array
  1506. [inner_index];
  1507. m_index++;
  1508. candidate_array[inner_index] =
  1509. (void*)1;
  1510. member_found = 1;
  1511. }
  1512. inner_index++;
  1513. }
  1514. outer_index++;
  1515. }
  1516. }
  1517. /* here we are left only with values to delete
  1518. from the memberof attribute in the candidate list
  1519. */
  1520. outer_index = 0;
  1521. while(outer_index < c_index)
  1522. {
  1523. /* Check for a special value in this position
  1524. * that indicates that the candidate was moved
  1525. * to the member array. */
  1526. if((void*)1 == candidate_array[outer_index])
  1527. {
  1528. /* item moved, skip */
  1529. outer_index++;
  1530. continue;
  1531. }
  1532. memberof_del_one(
  1533. 0, config,
  1534. (char*)slapi_value_get_string(
  1535. candidate_array[outer_index]),
  1536. (char*)slapi_value_get_string(entry_dn));
  1537. outer_index++;
  1538. }
  1539. {
  1540. /* crazyness follows:
  1541. * strict-aliasing doesn't like the required cast
  1542. * to void for slapi_ch_free so we are made to
  1543. * juggle to get a normal thing done
  1544. */
  1545. void *pmember_array = member_array;
  1546. void *pcandidate_array = candidate_array;
  1547. slapi_ch_free(&pcandidate_array);
  1548. slapi_ch_free(&pmember_array);
  1549. candidate_array = 0;
  1550. member_array = 0;
  1551. }
  1552. }
  1553. }
  1554. bail:
  1555. slapi_value_free(&entry_dn);
  1556. return rc;
  1557. }
  1558. /*
  1559. * memberof_replace_list()
  1560. *
  1561. * Perform replace the group DN list in the memberof attribute of the list of targets
  1562. *
  1563. */
  1564. int memberof_replace_list(Slapi_PBlock *pb, MemberOfConfig *config, char *group_dn)
  1565. {
  1566. struct slapi_entry *pre_e = NULL;
  1567. struct slapi_entry *post_e = NULL;
  1568. Slapi_Attr *pre_attr = 0;
  1569. Slapi_Attr *post_attr = 0;
  1570. int i = 0;
  1571. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &pre_e );
  1572. slapi_pblock_get( pb, SLAPI_ENTRY_POST_OP, &post_e );
  1573. for (i = 0; config && config->groupattrs[i]; i++)
  1574. {
  1575. if(pre_e && post_e)
  1576. {
  1577. slapi_entry_attr_find( pre_e, config->groupattrs[i], &pre_attr );
  1578. slapi_entry_attr_find( post_e, config->groupattrs[i], &post_attr );
  1579. }
  1580. if(pre_attr || post_attr)
  1581. {
  1582. int pre_total = 0;
  1583. int post_total = 0;
  1584. Slapi_Value **pre_array = 0;
  1585. Slapi_Value **post_array = 0;
  1586. int pre_index = 0;
  1587. int post_index = 0;
  1588. /* create arrays of values */
  1589. if(pre_attr)
  1590. {
  1591. slapi_attr_get_numvalues( pre_attr, &pre_total);
  1592. }
  1593. if(post_attr)
  1594. {
  1595. slapi_attr_get_numvalues( post_attr, &post_total);
  1596. }
  1597. /* Stash a plugin global pointer here and have memberof_qsort_compare
  1598. * use it. We have to do this because we use memberof_qsort_compare
  1599. * as the comparator function for qsort, which requires the function
  1600. * to only take two void* args. This is thread-safe since we only
  1601. * store and use the pointer while holding the memberOf operation
  1602. * lock. */
  1603. qsortConfig = config;
  1604. if(pre_total)
  1605. {
  1606. pre_array =
  1607. (Slapi_Value**)
  1608. slapi_ch_malloc(sizeof(Slapi_Value*)*pre_total);
  1609. memberof_load_array(pre_array, pre_attr);
  1610. qsort(
  1611. pre_array,
  1612. pre_total,
  1613. sizeof(Slapi_Value*),
  1614. memberof_qsort_compare);
  1615. }
  1616. if(post_total)
  1617. {
  1618. post_array =
  1619. (Slapi_Value**)
  1620. slapi_ch_malloc(sizeof(Slapi_Value*)*post_total);
  1621. memberof_load_array(post_array, post_attr);
  1622. qsort(
  1623. post_array,
  1624. post_total,
  1625. sizeof(Slapi_Value*),
  1626. memberof_qsort_compare);
  1627. }
  1628. qsortConfig = 0;
  1629. /* work through arrays, following these rules:
  1630. in pre, in post, do nothing
  1631. in pre, not in post, delete from entry
  1632. not in pre, in post, add to entry
  1633. */
  1634. while(pre_index < pre_total || post_index < post_total)
  1635. {
  1636. if(pre_index == pre_total)
  1637. {
  1638. /* add the rest of post */
  1639. memberof_add_one(
  1640. pb, config,
  1641. group_dn,
  1642. (char*)slapi_value_get_string(
  1643. post_array[post_index]));
  1644. post_index++;
  1645. }
  1646. else if(post_index == post_total)
  1647. {
  1648. /* delete the rest of pre */
  1649. memberof_del_one(
  1650. pb, config,
  1651. group_dn,
  1652. (char*)slapi_value_get_string(
  1653. pre_array[pre_index]));
  1654. pre_index++;
  1655. }
  1656. else
  1657. {
  1658. /* decide what to do */
  1659. int cmp = memberof_compare(
  1660. config,
  1661. &(pre_array[pre_index]),
  1662. &(post_array[post_index]));
  1663. if(cmp < 0)
  1664. {
  1665. /* delete pre array */
  1666. memberof_del_one(
  1667. pb, config,
  1668. group_dn,
  1669. (char*)slapi_value_get_string(
  1670. pre_array[pre_index]));
  1671. pre_index++;
  1672. }
  1673. else if(cmp > 0)
  1674. {
  1675. /* add post array */
  1676. memberof_add_one(
  1677. pb, config,
  1678. group_dn,
  1679. (char*)slapi_value_get_string(
  1680. post_array[post_index]));
  1681. post_index++;
  1682. }
  1683. else
  1684. {
  1685. /* do nothing, advance */
  1686. pre_index++;
  1687. post_index++;
  1688. }
  1689. }
  1690. }
  1691. slapi_ch_free((void **)&pre_array);
  1692. slapi_ch_free((void **)&post_array);
  1693. }
  1694. }
  1695. return 0;
  1696. }
  1697. /* memberof_load_array()
  1698. *
  1699. * put attribute values in array structure
  1700. */
  1701. void memberof_load_array(Slapi_Value **array, Slapi_Attr *attr)
  1702. {
  1703. Slapi_Value *val = 0;
  1704. int hint = slapi_attr_first_value(attr, &val);
  1705. while(val)
  1706. {
  1707. *array = val;
  1708. array++;
  1709. hint = slapi_attr_next_value(attr, hint, &val);
  1710. }
  1711. }
  1712. /* memberof_compare()
  1713. *
  1714. * compare two attr values
  1715. */
  1716. int memberof_compare(MemberOfConfig *config, const void *a, const void *b)
  1717. {
  1718. Slapi_Value *val1 = *((Slapi_Value **)a);
  1719. Slapi_Value *val2 = *((Slapi_Value **)b);
  1720. /* We only need to provide a Slapi_Attr here for it's syntax. We
  1721. * already validated all grouping attributes to use the Distinguished
  1722. * Name syntax, so we can safely just use the first attr. */
  1723. return slapi_attr_value_cmp(
  1724. config->group_slapiattrs[0],
  1725. slapi_value_get_berval(val1),
  1726. slapi_value_get_berval(val2));
  1727. }
  1728. /* memberof_qsort_compare()
  1729. *
  1730. * This is a version of memberof_compare that uses a plugin
  1731. * global copy of the config. We'd prefer to pass in a copy
  1732. * of config that is local to the running thread, but we can't
  1733. * do this since qsort is using us as a comparator function.
  1734. * We should only use this function when using qsort, and only
  1735. * when the memberOf lock is acquired.
  1736. */
  1737. int memberof_qsort_compare(const void *a, const void *b)
  1738. {
  1739. Slapi_Value *val1 = *((Slapi_Value **)a);
  1740. Slapi_Value *val2 = *((Slapi_Value **)b);
  1741. /* We only need to provide a Slapi_Attr here for it's syntax. We
  1742. * already validated all grouping attributes to use the Distinguished
  1743. * Name syntax, so we can safely just use the first attr. */
  1744. return slapi_attr_value_cmp(
  1745. qsortConfig->group_slapiattrs[0],
  1746. slapi_value_get_berval(val1),
  1747. slapi_value_get_berval(val2));
  1748. }
  1749. void memberof_lock()
  1750. {
  1751. slapi_lock_mutex(memberof_operation_lock);
  1752. }
  1753. void memberof_unlock()
  1754. {
  1755. slapi_unlock_mutex(memberof_operation_lock);
  1756. }
  1757. typedef struct _task_data
  1758. {
  1759. char *dn;
  1760. char *filter_str;
  1761. } task_data;
  1762. void memberof_fixup_task_thread(void *arg)
  1763. {
  1764. MemberOfConfig configCopy = {0, 0, 0, 0};
  1765. Slapi_Task *task = (Slapi_Task *)arg;
  1766. task_data *td = NULL;
  1767. int rc = 0;
  1768. /* Fetch our task data from the task */
  1769. td = (task_data *)slapi_task_get_data(task);
  1770. slapi_task_begin(task, 1);
  1771. slapi_task_log_notice(task, "Memberof task starts (arg: %s) ...\n",
  1772. td->filter_str);
  1773. /* We need to get the config lock first. Trying to get the
  1774. * config lock after we already hold the op lock can cause
  1775. * a deadlock. */
  1776. memberof_rlock_config();
  1777. /* copy config so it doesn't change out from under us */
  1778. memberof_copy_config(&configCopy, memberof_get_config());
  1779. memberof_unlock_config();
  1780. /* get the memberOf operation lock */
  1781. memberof_lock();
  1782. /* do real work */
  1783. rc = memberof_fix_memberof(&configCopy, td->dn, td->filter_str);
  1784. /* release the memberOf operation lock */
  1785. memberof_unlock();
  1786. memberof_free_config(&configCopy);
  1787. slapi_task_log_notice(task, "Memberof task finished.");
  1788. slapi_task_log_status(task, "Memberof task finished.");
  1789. slapi_task_inc_progress(task);
  1790. /* this will queue the destruction of the task */
  1791. slapi_task_finish(task, rc);
  1792. }
  1793. /* extract a single value from the entry (as a string) -- if it's not in the
  1794. * entry, the default will be returned (which can be NULL).
  1795. * you do not need to free anything returned by this.
  1796. */
  1797. const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  1798. const char *default_val)
  1799. {
  1800. Slapi_Attr *attr;
  1801. Slapi_Value *val = NULL;
  1802. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  1803. return default_val;
  1804. slapi_attr_first_value(attr, &val);
  1805. return slapi_value_get_string(val);
  1806. }
  1807. int memberof_task_add(Slapi_PBlock *pb, Slapi_Entry *e,
  1808. Slapi_Entry *eAfter, int *returncode, char *returntext,
  1809. void *arg)
  1810. {
  1811. PRThread *thread = NULL;
  1812. int rv = SLAPI_DSE_CALLBACK_OK;
  1813. task_data *mytaskdata = NULL;
  1814. Slapi_Task *task = NULL;
  1815. const char *filter;
  1816. const char *dn = 0;
  1817. *returncode = LDAP_SUCCESS;
  1818. /* get arg(s) */
  1819. if ((dn = fetch_attr(e, "basedn", 0)) == NULL)
  1820. {
  1821. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1822. rv = SLAPI_DSE_CALLBACK_ERROR;
  1823. goto out;
  1824. }
  1825. if ((filter = fetch_attr(e, "filter", "(objectclass=inetuser)")) == NULL)
  1826. {
  1827. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1828. rv = SLAPI_DSE_CALLBACK_ERROR;
  1829. goto out;
  1830. }
  1831. /* setup our task data */
  1832. mytaskdata = (task_data*)slapi_ch_malloc(sizeof(task_data));
  1833. if (mytaskdata == NULL)
  1834. {
  1835. *returncode = LDAP_OPERATIONS_ERROR;
  1836. rv = SLAPI_DSE_CALLBACK_ERROR;
  1837. goto out;
  1838. }
  1839. mytaskdata->dn = slapi_ch_strdup(dn);
  1840. mytaskdata->filter_str = slapi_ch_strdup(filter);
  1841. /* allocate new task now */
  1842. task = slapi_new_task(slapi_entry_get_ndn(e));
  1843. /* register our destructor for cleaning up our private data */
  1844. slapi_task_set_destructor_fn(task, memberof_task_destructor);
  1845. /* Stash a pointer to our data in the task */
  1846. slapi_task_set_data(task, mytaskdata);
  1847. /* start the sample task as a separate thread */
  1848. thread = PR_CreateThread(PR_USER_THREAD, memberof_fixup_task_thread,
  1849. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1850. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1851. if (thread == NULL)
  1852. {
  1853. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  1854. "unable to create task thread!\n");
  1855. *returncode = LDAP_OPERATIONS_ERROR;
  1856. rv = SLAPI_DSE_CALLBACK_ERROR;
  1857. slapi_task_finish(task, *returncode);
  1858. } else {
  1859. rv = SLAPI_DSE_CALLBACK_OK;
  1860. }
  1861. out:
  1862. return rv;
  1863. }
  1864. void
  1865. memberof_task_destructor(Slapi_Task *task)
  1866. {
  1867. if (task) {
  1868. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1869. if (mydata) {
  1870. slapi_ch_free_string(&mydata->dn);
  1871. slapi_ch_free_string(&mydata->filter_str);
  1872. /* Need to cast to avoid a compiler warning */
  1873. slapi_ch_free((void **)&mydata);
  1874. }
  1875. }
  1876. }
  1877. int memberof_fix_memberof(MemberOfConfig *config, char *dn, char *filter_str)
  1878. {
  1879. int rc = 0;
  1880. Slapi_PBlock *search_pb = slapi_pblock_new();
  1881. slapi_search_internal_set_pb(search_pb, dn,
  1882. LDAP_SCOPE_SUBTREE, filter_str, 0, 0,
  1883. 0, 0,
  1884. memberof_get_plugin_id(),
  1885. 0);
  1886. rc = slapi_search_internal_callback_pb(search_pb,
  1887. config,
  1888. 0, memberof_fix_memberof_callback,
  1889. 0);
  1890. slapi_pblock_destroy(search_pb);
  1891. return rc;
  1892. }
  1893. /* memberof_fix_memberof_callback()
  1894. * Add initial and/or fix up broken group list in entry
  1895. *
  1896. * 1. Remove all present memberOf values
  1897. * 2. Add direct group membership memberOf values
  1898. * 3. Add indirect group membership memberOf values
  1899. */
  1900. int memberof_fix_memberof_callback(Slapi_Entry *e, void *callback_data)
  1901. {
  1902. int rc = 0;
  1903. char *dn = slapi_entry_get_dn(e);
  1904. MemberOfConfig *config = (MemberOfConfig *)callback_data;
  1905. memberof_del_dn_data del_data = {0, config->memberof_attr};
  1906. Slapi_ValueSet *groups = 0;
  1907. /* get a list of all of the groups this user belongs to */
  1908. groups = memberof_get_groups(config, dn);
  1909. /* If we found some groups, replace the existing memberOf attribute
  1910. * with the found values. */
  1911. if (groups && slapi_valueset_count(groups))
  1912. {
  1913. Slapi_PBlock *mod_pb = slapi_pblock_new();
  1914. Slapi_Value *val = 0;
  1915. Slapi_Mod *smod;
  1916. LDAPMod **mods = (LDAPMod **) slapi_ch_malloc(2 * sizeof(LDAPMod *));
  1917. int hint = 0;
  1918. smod = slapi_mod_new();
  1919. slapi_mod_init(smod, 0);
  1920. slapi_mod_set_operation(smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  1921. slapi_mod_set_type(smod, config->memberof_attr);
  1922. /* Loop through all of our values and add them to smod */
  1923. hint = slapi_valueset_first_value(groups, &val);
  1924. while (val)
  1925. {
  1926. /* this makes a copy of the berval */
  1927. slapi_mod_add_value(smod, slapi_value_get_berval(val));
  1928. hint = slapi_valueset_next_value(groups, hint, &val);
  1929. }
  1930. mods[0] = slapi_mod_get_ldapmod_passout(smod);
  1931. mods[1] = 0;
  1932. slapi_modify_internal_set_pb(
  1933. mod_pb, dn, mods, 0, 0,
  1934. memberof_get_plugin_id(), 0);
  1935. slapi_modify_internal_pb(mod_pb);
  1936. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1937. ldap_mods_free(mods, 1);
  1938. slapi_mod_free(&smod);
  1939. slapi_pblock_destroy(mod_pb);
  1940. } else {
  1941. /* No groups were found, so remove the memberOf attribute
  1942. * from this entry. */
  1943. memberof_del_dn_type_callback(e, &del_data);
  1944. }
  1945. slapi_valueset_free(groups);
  1946. return rc;
  1947. }