memberof.c 56 KB

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