memberof.c 56 KB

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