repl5_updatedn_list.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * END COPYRIGHT BLOCK **/
  6. /* repl5_updatedn_list.c */
  7. /*
  8. This is the internal representation for the list of update DNs in the replica.
  9. The list is implemented as a hash table - the key is the normalized DN, and the
  10. value is the Slapi_DN representation of the DN
  11. */
  12. #include "repl5.h"
  13. #include "plhash.h"
  14. /* global data */
  15. /* typedef ReplicaUpdateDNList PLHashTable; */
  16. struct repl_enum_data
  17. {
  18. FNEnumDN fn;
  19. void *arg;
  20. };
  21. /* Forward declarations */
  22. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg);
  23. static PRIntn updatedn_list_enumerate (PLHashEntry *he, PRIntn index, void *hash_data);
  24. static int
  25. updatedn_compare_dns(const void *d1, const void *d2)
  26. {
  27. return (0 == slapi_sdn_compare((const Slapi_DN *)d1, (const Slapi_DN *)d2));
  28. }
  29. /* create a new updatedn list - if the entry is given, initialize the list from
  30. the replicabinddn values given in the entry */
  31. ReplicaUpdateDNList
  32. replica_updatedn_list_new(const Slapi_Entry *entry)
  33. {
  34. /* allocate table */
  35. PLHashTable *hash = PL_NewHashTable(4, PL_HashString, PL_CompareStrings,
  36. updatedn_compare_dns, NULL, NULL);
  37. if (hash == NULL) {
  38. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_new_updatedn_list: "
  39. "failed to allocate hash table; NSPR error - %d\n",
  40. PR_GetError ());
  41. return NULL;
  42. }
  43. if (entry) {
  44. Slapi_Attr *attr = NULL;
  45. if (!slapi_entry_attr_find(entry, attr_replicaBindDn, &attr)) {
  46. Slapi_ValueSet *vs = NULL;
  47. slapi_attr_get_valueset(attr, &vs);
  48. replica_updatedn_list_replace(hash, vs);
  49. slapi_valueset_free(vs);
  50. }
  51. }
  52. return (ReplicaUpdateDNList)hash;
  53. }
  54. void
  55. replica_updatedn_list_free(ReplicaUpdateDNList list)
  56. {
  57. /* destroy the content */
  58. PLHashTable *hash = list;
  59. PL_HashTableEnumerateEntries(hash, replica_destroy_hash_entry, NULL);
  60. if (hash)
  61. PL_HashTableDestroy(hash);
  62. }
  63. void
  64. replica_updatedn_list_replace(ReplicaUpdateDNList list, const Slapi_ValueSet *vs)
  65. {
  66. replica_updatedn_list_delete(list, NULL); /* delete all values */
  67. replica_updatedn_list_add(list, vs);
  68. }
  69. /* if vs is given, delete only those values - otherwise, delete all values */
  70. void
  71. replica_updatedn_list_delete(ReplicaUpdateDNList list, const Slapi_ValueSet *vs)
  72. {
  73. PLHashTable *hash = list;
  74. if (!vs || slapi_valueset_count(vs) == 0) { /* just delete everything */
  75. PL_HashTableEnumerateEntries(hash, replica_destroy_hash_entry, NULL);
  76. } else {
  77. Slapi_ValueSet *vs_nc = (Slapi_ValueSet *)vs; /* cast away const */
  78. Slapi_Value *val = NULL;
  79. int index = 0;
  80. for (index = slapi_valueset_first_value(vs_nc, &val); val;
  81. index = slapi_valueset_next_value(vs_nc, index, &val)) {
  82. Slapi_DN *dn = slapi_sdn_new_dn_byval(slapi_value_get_string(val));
  83. /* locate object */
  84. Slapi_DN *deldn = (Slapi_DN *)PL_HashTableLookup(hash, slapi_sdn_get_ndn(dn));
  85. if (deldn == NULL)
  86. {
  87. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_updatedn_list_delete: "
  88. "update DN with value (%s) is not in the update DN list.\n",
  89. slapi_sdn_get_ndn(dn));
  90. } else {
  91. /* remove from hash */
  92. PL_HashTableRemove(hash, slapi_sdn_get_ndn(dn));
  93. /* free the pointer */
  94. slapi_sdn_free(&deldn);
  95. }
  96. /* free the temp dn */
  97. slapi_sdn_free(&dn);
  98. }
  99. }
  100. return;
  101. }
  102. void
  103. replica_updatedn_list_add(ReplicaUpdateDNList list, const Slapi_ValueSet *vs)
  104. {
  105. PLHashTable *hash = list;
  106. Slapi_ValueSet *vs_nc = (Slapi_ValueSet *)vs; /* cast away const */
  107. Slapi_Value *val = NULL;
  108. int index = 0;
  109. PR_ASSERT(list && vs);
  110. for (index = slapi_valueset_first_value(vs_nc, &val); val;
  111. index = slapi_valueset_next_value(vs_nc, index, &val)) {
  112. Slapi_DN *dn = slapi_sdn_new_dn_byval(slapi_value_get_string(val));
  113. const char *ndn = slapi_sdn_get_ndn(dn);
  114. /* make sure that the name is unique */
  115. if (PL_HashTableLookup(hash, ndn) != NULL)
  116. {
  117. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_updatedn_list_add: "
  118. "update DN with value (%s) already in the update DN list\n",
  119. ndn);
  120. slapi_sdn_free(&dn);
  121. } else {
  122. PL_HashTableAdd(hash, ndn, dn);
  123. }
  124. }
  125. return;
  126. }
  127. PRBool
  128. replica_updatedn_list_ismember(ReplicaUpdateDNList list, const Slapi_DN *dn)
  129. {
  130. PLHashTable *hash = list;
  131. PRBool ret = PR_FALSE;
  132. const char *ndn = slapi_sdn_get_ndn(dn);
  133. /* Bug 605169 - null ndn would cause core dump */
  134. if ( ndn ) {
  135. ret = (PRBool)PL_HashTableLookupConst(hash, ndn);
  136. }
  137. return ret;
  138. }
  139. struct list_to_string_data {
  140. char *string;
  141. const char *delimiter;
  142. };
  143. static int
  144. convert_to_string(Slapi_DN *dn, void *arg)
  145. {
  146. struct list_to_string_data *data = (struct list_to_string_data *)arg;
  147. int newlen = strlen(slapi_sdn_get_dn(dn)) + strlen(data->delimiter) + 1;
  148. if (data->string) {
  149. newlen += strlen(data->string);
  150. data->string = slapi_ch_realloc(data->string, newlen);
  151. } else {
  152. data->string = slapi_ch_calloc(1, newlen);
  153. }
  154. strcat(data->string, slapi_sdn_get_dn(dn));
  155. strcat(data->string, data->delimiter);
  156. return 1;
  157. }
  158. /* caller must slapi_ch_free_string the returned string */
  159. char *
  160. replica_updatedn_list_to_string(ReplicaUpdateDNList list, const char *delimiter)
  161. {
  162. struct list_to_string_data data;
  163. data.string = NULL;
  164. data.delimiter = delimiter;
  165. replica_updatedn_list_enumerate(list, convert_to_string, (void *)&data);
  166. return data.string;
  167. }
  168. void
  169. replica_updatedn_list_enumerate(ReplicaUpdateDNList list, FNEnumDN fn, void *arg)
  170. {
  171. PLHashTable *hash = list;
  172. struct repl_enum_data data;
  173. PR_ASSERT (fn);
  174. data.fn = fn;
  175. data.arg = arg;
  176. PL_HashTableEnumerateEntries(hash, updatedn_list_enumerate, &data);
  177. }
  178. /* Helper functions */
  179. /* this function called for each hash node during hash destruction */
  180. static PRIntn
  181. replica_destroy_hash_entry(PLHashEntry *he, PRIntn index, void *arg)
  182. {
  183. Slapi_DN *dn = NULL;
  184. if (he == NULL)
  185. return HT_ENUMERATE_NEXT;
  186. dn = (Slapi_DN *)he->value;
  187. PR_ASSERT (dn);
  188. slapi_sdn_free(&dn);
  189. return HT_ENUMERATE_REMOVE;
  190. }
  191. static PRIntn
  192. updatedn_list_enumerate(PLHashEntry *he, PRIntn index, void *hash_data)
  193. {
  194. Slapi_DN *dn = NULL;
  195. struct repl_enum_data *data = hash_data;
  196. dn = (Slapi_DN*)he->value;
  197. PR_ASSERT (dn);
  198. data->fn(dn, data->arg);
  199. return HT_ENUMERATE_NEXT;
  200. }