repl5_updatedn_list.c 8.3 KB

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