memberof.c 75 KB

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