repl5_updatedn_list.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_updatedn_list.c */
  13. /*
  14. This is the internal representation for the list of update DNs in the replica.
  15. The list is implemented as a hash table - the key is the normalized DN, and the
  16. value is the Slapi_DN representation of the DN
  17. */
  18. #include "repl5.h"
  19. #include "plhash.h"
  20. /* global data */
  21. /* typedef ReplicaUpdateDNList PLHashTable; */
  22. struct repl_enum_data
  23. {
  24. FNEnumDN fn;
  25. void *arg;
  26. };
  27. /* Forward declarations */
  28. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg);
  29. static PRIntn updatedn_list_enumerate (PLHashEntry *he, PRIntn index, void *hash_data);
  30. static int
  31. updatedn_compare_dns(const void *d1, const void *d2)
  32. {
  33. return (0 == slapi_sdn_compare((const Slapi_DN *)d1, (const Slapi_DN *)d2));
  34. }
  35. /* create a new updatedn list - if the entry is given, initialize the list from
  36. the replicabinddn values given in the entry */
  37. ReplicaUpdateDNList
  38. replica_updatedn_list_new(const Slapi_Entry *entry)
  39. {
  40. /* allocate table */
  41. PLHashTable *hash = PL_NewHashTable(4, PL_HashString, PL_CompareStrings,
  42. updatedn_compare_dns, NULL, NULL);
  43. if (hash == NULL) {
  44. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_new_updatedn_list: "
  45. "failed to allocate hash table; NSPR error - %d\n",
  46. PR_GetError ());
  47. return NULL;
  48. }
  49. if (entry) {
  50. Slapi_Attr *attr = NULL;
  51. if (!slapi_entry_attr_find(entry, attr_replicaBindDn, &attr)) {
  52. Slapi_ValueSet *vs = NULL;
  53. slapi_attr_get_valueset(attr, &vs);
  54. replica_updatedn_list_replace(hash, vs);
  55. slapi_valueset_free(vs);
  56. }
  57. }
  58. return (ReplicaUpdateDNList)hash;
  59. }
  60. Slapi_ValueSet *
  61. replica_updatedn_group_new(const Slapi_Entry *entry)
  62. {
  63. Slapi_ValueSet *vs = NULL;
  64. if (entry) {
  65. Slapi_Attr *attr = NULL;
  66. if (!slapi_entry_attr_find(entry, attr_replicaBindDnGroup, &attr)) {
  67. slapi_attr_get_valueset(attr, &vs);
  68. }
  69. }
  70. return (vs);
  71. }
  72. ReplicaUpdateDNList
  73. replica_groupdn_list_new(const Slapi_ValueSet *vs)
  74. {
  75. PLHashTable *hash;
  76. if (vs == NULL) {
  77. return NULL;
  78. }
  79. /* allocate table */
  80. hash = PL_NewHashTable(4, PL_HashString, PL_CompareStrings,
  81. updatedn_compare_dns, NULL, NULL);
  82. if (hash == NULL) {
  83. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_new_updatedn_list: "
  84. "failed to allocate hash table; NSPR error - %d\n",
  85. PR_GetError ());
  86. return NULL;
  87. }
  88. replica_updatedn_list_delete(hash, NULL); /* delete all values */
  89. replica_updatedn_list_add_ext(hash, vs, 1);
  90. return (ReplicaUpdateDNList)hash;
  91. }
  92. void
  93. replica_updatedn_list_free(ReplicaUpdateDNList list)
  94. {
  95. /* destroy the content */
  96. PLHashTable *hash = list;
  97. PL_HashTableEnumerateEntries(hash, replica_destroy_hash_entry, NULL);
  98. if (hash)
  99. PL_HashTableDestroy(hash);
  100. }
  101. void
  102. replica_updatedn_list_replace(ReplicaUpdateDNList list, const Slapi_ValueSet *vs)
  103. {
  104. replica_updatedn_list_delete(list, NULL); /* delete all values */
  105. replica_updatedn_list_add_ext(list, vs, 0);
  106. }
  107. void
  108. replica_updatedn_list_group_replace(ReplicaUpdateDNList list, const Slapi_ValueSet *vs)
  109. {
  110. replica_updatedn_list_delete(list, NULL); /* delete all values */
  111. replica_updatedn_list_add_ext(list, vs, 1);
  112. }
  113. /* if vs is given, delete only those values - otherwise, delete all values */
  114. void
  115. replica_updatedn_list_delete(ReplicaUpdateDNList list, const Slapi_ValueSet *vs)
  116. {
  117. PLHashTable *hash = list;
  118. if (!vs || slapi_valueset_count(vs) == 0) { /* just delete everything */
  119. PL_HashTableEnumerateEntries(hash, replica_destroy_hash_entry, NULL);
  120. } else {
  121. Slapi_ValueSet *vs_nc = (Slapi_ValueSet *)vs; /* cast away const */
  122. Slapi_Value *val = NULL;
  123. int index = 0;
  124. for (index = slapi_valueset_first_value(vs_nc, &val); val;
  125. index = slapi_valueset_next_value(vs_nc, index, &val)) {
  126. Slapi_DN *dn = slapi_sdn_new_dn_byval(slapi_value_get_string(val));
  127. /* locate object */
  128. Slapi_DN *deldn = (Slapi_DN *)PL_HashTableLookup(hash, slapi_sdn_get_ndn(dn));
  129. if (deldn == NULL)
  130. {
  131. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_updatedn_list_delete: "
  132. "update DN with value (%s) is not in the update DN list.\n",
  133. slapi_sdn_get_ndn(dn));
  134. } else {
  135. /* remove from hash */
  136. PL_HashTableRemove(hash, slapi_sdn_get_ndn(dn));
  137. /* free the pointer */
  138. slapi_sdn_free(&deldn);
  139. }
  140. /* free the temp dn */
  141. slapi_sdn_free(&dn);
  142. }
  143. }
  144. return;
  145. }
  146. Slapi_ValueSet *
  147. replica_updatedn_list_get_members(Slapi_DN *dn)
  148. {
  149. static char* const filter_groups = "(|(objectclass=groupOfNames)(objectclass=groupOfUniqueNames)(objectclass=groupOfURLs))";
  150. static char* const type_member = "member";
  151. static char* const type_uniquemember = "uniquemember";
  152. static char* const type_memberURL = "memberURL";
  153. int rval;
  154. char *attrs[4];
  155. Slapi_PBlock *mpb = slapi_pblock_new ();
  156. Slapi_ValueSet *members = slapi_valueset_new();
  157. attrs[0] = type_member;
  158. attrs[1] = type_uniquemember;
  159. attrs[2] = type_memberURL;
  160. attrs[3] = NULL;
  161. slapi_search_internal_set_pb ( mpb, slapi_sdn_get_ndn(dn), LDAP_SCOPE_BASE, filter_groups,
  162. &attrs[0], 0, NULL /* controls */, NULL /* uniqueid */,
  163. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  164. slapi_search_internal_pb(mpb);
  165. slapi_pblock_get(mpb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
  166. if (rval == LDAP_SUCCESS) {
  167. Slapi_Entry **ep;
  168. slapi_pblock_get(mpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &ep);
  169. if ((ep != NULL) && (ep[0] != NULL)) {
  170. Slapi_Attr *attr = NULL;
  171. Slapi_Attr *nextAttr = NULL;
  172. Slapi_ValueSet *vs = NULL;
  173. char *attrType;
  174. slapi_entry_first_attr ( ep[0], &attr);
  175. while (attr) {
  176. slapi_attr_get_type ( attr, &attrType );
  177. if ((strcasecmp (attrType, type_member) == 0) ||
  178. (strcasecmp (attrType, type_uniquemember) == 0 )) {
  179. slapi_attr_get_valueset(attr, &vs);
  180. slapi_valueset_join_attr_valueset(attr, members, vs);
  181. slapi_valueset_free(vs);
  182. } else if (strcasecmp (attrType, type_memberURL) == 0) {
  183. /* not yet supported */
  184. }
  185. slapi_entry_next_attr ( ep[0], attr, &nextAttr );
  186. attr = nextAttr;
  187. }
  188. }
  189. }
  190. slapi_free_search_results_internal(mpb);
  191. slapi_pblock_destroy (mpb);
  192. return(members);
  193. }
  194. /*
  195. * add a list of dns to the ReplicaUpdateDNList.
  196. * The dn could be the dn of a group, so get the entry
  197. * and check the objectclass. If it is a static or dynamic group
  198. * generate the list of member dns and recursively call
  199. * replica_updatedn_list_add().
  200. * The dn of the group is added to the list, so it will detect
  201. * potential circular group definitions
  202. */
  203. void
  204. replica_updatedn_list_add_ext(ReplicaUpdateDNList list, const Slapi_ValueSet *vs, int group_update)
  205. {
  206. PLHashTable *hash = list;
  207. Slapi_ValueSet *vs_nc = (Slapi_ValueSet *)vs; /* cast away const */
  208. Slapi_Value *val = NULL;
  209. int index = 0;
  210. PR_ASSERT(list && vs);
  211. for (index = slapi_valueset_first_value(vs_nc, &val); val;
  212. index = slapi_valueset_next_value(vs_nc, index, &val)) {
  213. Slapi_DN *dn = slapi_sdn_new_dn_byval(slapi_value_get_string(val));
  214. const char *ndn = slapi_sdn_get_ndn(dn);
  215. /* make sure that the name is unique */
  216. if (PL_HashTableLookup(hash, ndn) != NULL)
  217. {
  218. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_updatedn_list_add: "
  219. "update DN with value (%s) already in the update DN list\n",
  220. ndn);
  221. slapi_sdn_free(&dn);
  222. } else {
  223. Slapi_ValueSet *members = NULL;
  224. PL_HashTableAdd(hash, ndn, dn);
  225. /* add it, even if it is a group dn, this will
  226. * prevent problems with circular group definitions
  227. * then check if it has mor members to add */
  228. if (group_update) {
  229. members = replica_updatedn_list_get_members(dn);
  230. if (members) {
  231. replica_updatedn_list_add_ext(list, members, 1);
  232. /* free members */
  233. slapi_valueset_free(members);
  234. }
  235. }
  236. }
  237. }
  238. return;
  239. }
  240. void
  241. replica_updatedn_list_add(ReplicaUpdateDNList list, const Slapi_ValueSet *vs)
  242. {
  243. replica_updatedn_list_add_ext(list, vs, 0);
  244. }
  245. PRBool
  246. replica_updatedn_list_ismember(ReplicaUpdateDNList list, const Slapi_DN *dn)
  247. {
  248. PLHashTable *hash = list;
  249. PRBool ret = PR_FALSE;
  250. const char *ndn = slapi_sdn_get_ndn(dn);
  251. /* Bug 605169 - null ndn would cause core dump */
  252. if ( ndn ) {
  253. if ((uintptr_t)PL_HashTableLookupConst(hash, ndn))
  254. ret = PR_TRUE;
  255. else
  256. ret = PR_FALSE;
  257. }
  258. return ret;
  259. }
  260. struct list_to_string_data {
  261. char *string;
  262. const char *delimiter;
  263. };
  264. static int
  265. convert_to_string(Slapi_DN *dn, void *arg)
  266. {
  267. struct list_to_string_data *data = (struct list_to_string_data *)arg;
  268. int newlen = slapi_sdn_get_ndn_len(dn) + strlen(data->delimiter) + 1;
  269. if (data->string) {
  270. newlen += strlen(data->string);
  271. data->string = slapi_ch_realloc(data->string, newlen);
  272. } else {
  273. data->string = slapi_ch_calloc(1, newlen);
  274. }
  275. strcat(data->string, slapi_sdn_get_dn(dn));
  276. strcat(data->string, data->delimiter);
  277. return 1;
  278. }
  279. /* caller must slapi_ch_free_string the returned string */
  280. char *
  281. replica_updatedn_list_to_string(ReplicaUpdateDNList list, const char *delimiter)
  282. {
  283. struct list_to_string_data data;
  284. data.string = NULL;
  285. data.delimiter = delimiter;
  286. replica_updatedn_list_enumerate(list, convert_to_string, (void *)&data);
  287. return data.string;
  288. }
  289. void
  290. replica_updatedn_list_enumerate(ReplicaUpdateDNList list, FNEnumDN fn, void *arg)
  291. {
  292. PLHashTable *hash = list;
  293. struct repl_enum_data data;
  294. PR_ASSERT (fn);
  295. data.fn = fn;
  296. data.arg = arg;
  297. PL_HashTableEnumerateEntries(hash, updatedn_list_enumerate, &data);
  298. }
  299. /* Helper functions */
  300. /* this function called for each hash node during hash destruction */
  301. static PRIntn
  302. replica_destroy_hash_entry(PLHashEntry *he, PRIntn index, void *arg)
  303. {
  304. Slapi_DN *dn = NULL;
  305. if (he == NULL)
  306. return HT_ENUMERATE_NEXT;
  307. dn = (Slapi_DN *)he->value;
  308. PR_ASSERT (dn);
  309. slapi_sdn_free(&dn);
  310. return HT_ENUMERATE_REMOVE;
  311. }
  312. static PRIntn
  313. updatedn_list_enumerate(PLHashEntry *he, PRIntn index, void *hash_data)
  314. {
  315. Slapi_DN *dn = NULL;
  316. struct repl_enum_data *data = hash_data;
  317. dn = (Slapi_DN*)he->value;
  318. PR_ASSERT (dn);
  319. data->fn(dn, data->arg);
  320. return HT_ENUMERATE_NEXT;
  321. }