repl5_updatedn_list.c 12 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_err(SLAPI_LOG_ERR, 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_err(SLAPI_LOG_ERR, 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. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_updatedn_list_delete -"
  131. "Update DN with value (%s) is not in the update DN list.\n",
  132. slapi_sdn_get_ndn(dn));
  133. } else {
  134. /* remove from hash */
  135. PL_HashTableRemove(hash, slapi_sdn_get_ndn(dn));
  136. /* free the pointer */
  137. slapi_sdn_free(&deldn);
  138. }
  139. /* free the temp dn */
  140. slapi_sdn_free(&dn);
  141. }
  142. }
  143. return;
  144. }
  145. Slapi_ValueSet *
  146. replica_updatedn_list_get_members(Slapi_DN *dn)
  147. {
  148. static char *const filter_groups = "(|(objectclass=groupOfNames)(objectclass=groupOfUniqueNames)(objectclass=groupOfURLs))";
  149. static char *const type_member = "member";
  150. static char *const type_uniquemember = "uniquemember";
  151. static char *const type_memberURL = "memberURL";
  152. int rval;
  153. char *attrs[4];
  154. Slapi_PBlock *mpb = slapi_pblock_new();
  155. Slapi_ValueSet *members = slapi_valueset_new();
  156. attrs[0] = type_member;
  157. attrs[1] = type_uniquemember;
  158. attrs[2] = type_memberURL;
  159. attrs[3] = NULL;
  160. slapi_search_internal_set_pb(mpb, slapi_sdn_get_ndn(dn), LDAP_SCOPE_BASE, filter_groups,
  161. &attrs[0], 0, NULL /* controls */, NULL /* uniqueid */,
  162. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  163. slapi_search_internal_pb(mpb);
  164. slapi_pblock_get(mpb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
  165. if (rval == LDAP_SUCCESS) {
  166. Slapi_Entry **ep;
  167. slapi_pblock_get(mpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &ep);
  168. if ((ep != NULL) && (ep[0] != NULL)) {
  169. Slapi_Attr *attr = NULL;
  170. Slapi_Attr *nextAttr = NULL;
  171. Slapi_ValueSet *vs = NULL;
  172. char *attrType;
  173. slapi_entry_first_attr(ep[0], &attr);
  174. while (attr) {
  175. slapi_attr_get_type(attr, &attrType);
  176. if ((strcasecmp(attrType, type_member) == 0) ||
  177. (strcasecmp(attrType, type_uniquemember) == 0)) {
  178. slapi_attr_get_valueset(attr, &vs);
  179. slapi_valueset_join_attr_valueset(attr, members, vs);
  180. slapi_valueset_free(vs);
  181. } else if (strcasecmp(attrType, type_memberURL) == 0) {
  182. /* not yet supported */
  183. }
  184. slapi_entry_next_attr(ep[0], attr, &nextAttr);
  185. attr = nextAttr;
  186. }
  187. }
  188. }
  189. slapi_free_search_results_internal(mpb);
  190. slapi_pblock_destroy(mpb);
  191. return (members);
  192. }
  193. /*
  194. * add a list of dns to the ReplicaUpdateDNList.
  195. * The dn could be the dn of a group, so get the entry
  196. * and check the objectclass. If it is a static or dynamic group
  197. * generate the list of member dns and recursively call
  198. * replica_updatedn_list_add().
  199. * The dn of the group is added to the list, so it will detect
  200. * potential circular group definitions
  201. */
  202. void
  203. replica_updatedn_list_add_ext(ReplicaUpdateDNList list, const Slapi_ValueSet *vs, int group_update)
  204. {
  205. PLHashTable *hash = list;
  206. Slapi_ValueSet *vs_nc = (Slapi_ValueSet *)vs; /* cast away const */
  207. Slapi_Value *val = NULL;
  208. int index = 0;
  209. PR_ASSERT(list && vs);
  210. for (index = slapi_valueset_first_value(vs_nc, &val); val;
  211. index = slapi_valueset_next_value(vs_nc, index, &val)) {
  212. Slapi_DN *dn = slapi_sdn_new_dn_byval(slapi_value_get_string(val));
  213. const char *ndn = slapi_sdn_get_ndn(dn);
  214. /* make sure that the name is unique */
  215. if (PL_HashTableLookup(hash, ndn) != NULL) {
  216. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_updatedn_list_add - "
  217. "Update DN with value (%s) already in the update DN list\n",
  218. ndn);
  219. slapi_sdn_free(&dn);
  220. } else {
  221. Slapi_ValueSet *members = NULL;
  222. PL_HashTableAdd(hash, ndn, dn);
  223. /* add it, even if it is a group dn, this will
  224. * prevent problems with circular group definitions
  225. * then check if it has mor members to add */
  226. if (group_update) {
  227. members = replica_updatedn_list_get_members(dn);
  228. if (members) {
  229. replica_updatedn_list_add_ext(list, members, 1);
  230. /* free members */
  231. slapi_valueset_free(members);
  232. }
  233. }
  234. }
  235. }
  236. return;
  237. }
  238. void
  239. replica_updatedn_list_add(ReplicaUpdateDNList list, const Slapi_ValueSet *vs)
  240. {
  241. replica_updatedn_list_add_ext(list, vs, 0);
  242. }
  243. PRBool
  244. replica_updatedn_list_ismember(ReplicaUpdateDNList list, const Slapi_DN *dn)
  245. {
  246. PLHashTable *hash = list;
  247. PRBool ret = PR_FALSE;
  248. const char *ndn = slapi_sdn_get_ndn(dn);
  249. /* Bug 605169 - null ndn would cause core dump */
  250. if (ndn) {
  251. if ((uintptr_t)PL_HashTableLookupConst(hash, ndn))
  252. ret = PR_TRUE;
  253. else
  254. ret = PR_FALSE;
  255. }
  256. return ret;
  257. }
  258. struct list_to_string_data
  259. {
  260. char *string;
  261. const char *delimiter;
  262. };
  263. static int
  264. convert_to_string(Slapi_DN *dn, void *arg)
  265. {
  266. struct list_to_string_data *data = (struct list_to_string_data *)arg;
  267. int newlen = slapi_sdn_get_ndn_len(dn) + strlen(data->delimiter) + 1;
  268. if (data->string) {
  269. newlen += strlen(data->string);
  270. data->string = slapi_ch_realloc(data->string, newlen);
  271. } else {
  272. data->string = slapi_ch_calloc(1, newlen);
  273. }
  274. strcat(data->string, slapi_sdn_get_dn(dn));
  275. strcat(data->string, data->delimiter);
  276. return 1;
  277. }
  278. /* caller must slapi_ch_free_string the returned string */
  279. char *
  280. replica_updatedn_list_to_string(ReplicaUpdateDNList list, const char *delimiter)
  281. {
  282. struct list_to_string_data data;
  283. data.string = NULL;
  284. data.delimiter = delimiter;
  285. replica_updatedn_list_enumerate(list, convert_to_string, (void *)&data);
  286. return data.string;
  287. }
  288. void
  289. replica_updatedn_list_enumerate(ReplicaUpdateDNList list, FNEnumDN fn, void *arg)
  290. {
  291. PLHashTable *hash = list;
  292. struct repl_enum_data data;
  293. PR_ASSERT(fn);
  294. data.fn = fn;
  295. data.arg = arg;
  296. PL_HashTableEnumerateEntries(hash, updatedn_list_enumerate, &data);
  297. }
  298. /* Helper functions */
  299. /* this function called for each hash node during hash destruction */
  300. static PRIntn
  301. replica_destroy_hash_entry(PLHashEntry *he, PRIntn index __attribute__((unused)), void *arg __attribute__((unused)))
  302. {
  303. Slapi_DN *dn = NULL;
  304. if (he == NULL) {
  305. return HT_ENUMERATE_NEXT;
  306. }
  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 __attribute__((unused)), 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. }