repl_controls.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include "slapi-plugin.h"
  13. #include "repl5.h"
  14. /*
  15. * repl_controls.c - convenience functions for creating and
  16. * decoding controls that implement 5.0-style replication
  17. * protocol operations.
  18. *
  19. * TODO: Send modrdn mods with modrdn operation
  20. * Fix ber_printf() and ber_scanf() format strings - some are
  21. * the wrong types.
  22. */
  23. /*
  24. * Return a pointer to a NSDS50ReplUpdateInfoControl.
  25. * The control looks like this:
  26. *
  27. * NSDS50ReplUpdateInfoControl ::= SEQUENCE {
  28. * uuid OCTET STRING,
  29. * csn OCTET STRING,
  30. * OPTIONAL [new]superior-uuid OCTET STRING
  31. * OPTIONAL modrdn_mods XXXggood WHAT TYPE???
  32. * }
  33. */
  34. int
  35. create_NSDS50ReplUpdateInfoControl(const char *uuid,
  36. const char *superior_uuid,
  37. const CSN *csn,
  38. LDAPMod **modrdn_mods,
  39. LDAPControl **ctrlp)
  40. {
  41. int retval;
  42. BerElement *tmp_bere = NULL;
  43. char csn_str[CSN_STRSIZE];
  44. if (NULL == ctrlp) {
  45. retval = LDAP_PARAM_ERROR;
  46. goto loser;
  47. } else {
  48. if ((tmp_bere = ber_alloc()) == NULL) {
  49. retval = LDAP_NO_MEMORY;
  50. goto loser;
  51. } else {
  52. /* Stuff uuid and csn into BerElement */
  53. if (ber_printf(tmp_bere, "{") == -1) {
  54. retval = LDAP_ENCODING_ERROR;
  55. goto loser;
  56. }
  57. /* Stuff uuid of this entry into BerElement */
  58. if (ber_printf(tmp_bere, "s", uuid) == -1) {
  59. retval = LDAP_ENCODING_ERROR;
  60. goto loser;
  61. }
  62. /* Stuff csn of this change into BerElement */
  63. csn_as_string(csn, PR_FALSE, csn_str);
  64. if (ber_printf(tmp_bere, "s", csn_str) == -1) {
  65. retval = LDAP_ENCODING_ERROR;
  66. goto loser;
  67. }
  68. /* If present, stuff uuid of parent entry into BerElement */
  69. if (NULL != superior_uuid) {
  70. if (ber_printf(tmp_bere, "s", superior_uuid) == -1) {
  71. retval = LDAP_ENCODING_ERROR;
  72. goto loser;
  73. }
  74. }
  75. /* If present, add the modrdn mods */
  76. if (NULL != modrdn_mods) {
  77. int i;
  78. if (ber_printf(tmp_bere, "{") == -1) {
  79. retval = LDAP_ENCODING_ERROR;
  80. goto loser;
  81. }
  82. /* for each modification to be performed... */
  83. for (i = 0; NULL != modrdn_mods[i]; i++) {
  84. if (ber_printf(tmp_bere, "{e{s[V]}}",
  85. modrdn_mods[i]->mod_op & ~LDAP_MOD_BVALUES,
  86. modrdn_mods[i]->mod_type, modrdn_mods[i]->mod_bvalues) == -1) {
  87. retval = LDAP_ENCODING_ERROR;
  88. goto loser;
  89. }
  90. }
  91. if (ber_printf(tmp_bere, "}") == -1) {
  92. retval = LDAP_ENCODING_ERROR;
  93. goto loser;
  94. }
  95. }
  96. /* Close the sequence */
  97. if (ber_printf(tmp_bere, "}") == -1) {
  98. retval = LDAP_ENCODING_ERROR;
  99. goto loser;
  100. }
  101. retval = slapi_build_control(REPL_NSDS50_UPDATE_INFO_CONTROL_OID,
  102. tmp_bere, 1 /* is critical */, ctrlp);
  103. }
  104. }
  105. loser:
  106. if (NULL != tmp_bere) {
  107. ber_free(tmp_bere, 1);
  108. tmp_bere = NULL;
  109. }
  110. return retval;
  111. }
  112. /*
  113. * Destroy a ReplUpdateInfoControl and set the pointer to NULL.
  114. */
  115. void
  116. destroy_NSDS50ReplUpdateInfoControl(LDAPControl **ctrlp)
  117. {
  118. if (NULL != ctrlp && NULL != *ctrlp) {
  119. ldap_control_free(*ctrlp);
  120. *ctrlp = NULL;
  121. }
  122. }
  123. /*
  124. * Look through the array of controls. If an NSDS50ReplUpdateInfoControl
  125. * is present, decode it and return pointers to the broken-out
  126. * components. The caller is responsible for freeing pointers to
  127. * the returned objects. The caller may indicate that it is not
  128. * interested in any of the output parameters by passing NULL
  129. * for that parameter.
  130. *
  131. * Returns 0 if the control is not present, 1 if it is present, and
  132. * -1 if an error occurs.
  133. */
  134. int
  135. decode_NSDS50ReplUpdateInfoControl(LDAPControl **controlsp,
  136. char **uuid,
  137. char **superior_uuid,
  138. CSN **csn,
  139. LDAPMod ***modrdn_mods)
  140. {
  141. struct berval *ctl_value = NULL;
  142. int iscritical = 0;
  143. int rc = -1;
  144. struct berval uuid_val = {0};
  145. struct berval superior_uuid_val = {0};
  146. struct berval csn_val = {0};
  147. BerElement *tmp_bere = NULL;
  148. Slapi_Mods modrdn_smods;
  149. PRBool got_modrdn_mods = PR_FALSE;
  150. ber_len_t len;
  151. slapi_mods_init(&modrdn_smods, 4);
  152. if (slapi_control_present(controlsp, REPL_NSDS50_UPDATE_INFO_CONTROL_OID,
  153. &ctl_value, &iscritical)) {
  154. if (!BV_HAS_DATA(ctl_value) || (tmp_bere = ber_init(ctl_value)) == NULL) {
  155. rc = -1;
  156. goto loser;
  157. }
  158. if (ber_scanf(tmp_bere, "{oo", &uuid_val, &csn_val) == LBER_ERROR) {
  159. rc = -1;
  160. goto loser;
  161. }
  162. if (ber_peek_tag(tmp_bere, &len) == LBER_OCTETSTRING) {
  163. /* The optional superior_uuid is present */
  164. if (ber_scanf(tmp_bere, "o", &superior_uuid_val) == LBER_DEFAULT) {
  165. rc = -1;
  166. goto loser;
  167. }
  168. }
  169. if (ber_peek_tag(tmp_bere, &len) == LBER_SEQUENCE) {
  170. ber_tag_t emtag;
  171. ber_len_t emlen;
  172. char *emlast;
  173. for (emtag = ber_first_element(tmp_bere, &emlen, &emlast);
  174. emtag != LBER_ERROR && emtag != LBER_END_OF_SEQORSET;
  175. emtag = ber_next_element(tmp_bere, &emlen, emlast)) {
  176. struct berval **embvals;
  177. ber_int_t op;
  178. char *type;
  179. if (ber_scanf(tmp_bere, "{i{a[V]}}", &op, &type, &embvals) == LBER_ERROR) {
  180. rc = -1;
  181. goto loser;
  182. }
  183. slapi_mods_add_modbvps(&modrdn_smods, op, type, embvals);
  184. slapi_ch_free_string(&type);
  185. ber_bvecfree(embvals);
  186. }
  187. got_modrdn_mods = PR_TRUE;
  188. }
  189. if (ber_scanf(tmp_bere, "}") == LBER_ERROR) {
  190. rc = -1;
  191. goto loser;
  192. }
  193. if (NULL != uuid) {
  194. *uuid = slapi_ch_malloc(uuid_val.bv_len + 1);
  195. strncpy(*uuid, uuid_val.bv_val, uuid_val.bv_len);
  196. (*uuid)[uuid_val.bv_len] = '\0';
  197. }
  198. if (NULL != csn) {
  199. char *csnstr = slapi_ch_malloc(csn_val.bv_len + 1);
  200. strncpy(csnstr, csn_val.bv_val, csn_val.bv_len);
  201. csnstr[csn_val.bv_len] = '\0';
  202. *csn = csn_new_by_string(csnstr);
  203. slapi_ch_free((void **)&csnstr);
  204. }
  205. if (NULL != superior_uuid && NULL != superior_uuid_val.bv_val) {
  206. *superior_uuid = slapi_ch_malloc(superior_uuid_val.bv_len + 1);
  207. strncpy(*superior_uuid, superior_uuid_val.bv_val,
  208. superior_uuid_val.bv_len);
  209. (*superior_uuid)[superior_uuid_val.bv_len] = '\0';
  210. }
  211. if (NULL != modrdn_mods && got_modrdn_mods) {
  212. *modrdn_mods = slapi_mods_get_ldapmods_passout(&modrdn_smods);
  213. }
  214. slapi_mods_done(&modrdn_smods);
  215. rc = 1;
  216. } else {
  217. rc = 0;
  218. }
  219. loser:
  220. /* XXXggood free CSN here if allocated */
  221. if (NULL != tmp_bere) {
  222. ber_free(tmp_bere, 1);
  223. tmp_bere = NULL;
  224. }
  225. if (NULL != uuid_val.bv_val) {
  226. ldap_memfree(uuid_val.bv_val);
  227. uuid_val.bv_val = NULL;
  228. }
  229. if (NULL != superior_uuid_val.bv_val) {
  230. ldap_memfree(superior_uuid_val.bv_val);
  231. superior_uuid_val.bv_val = NULL;
  232. }
  233. if (NULL != csn_val.bv_val) {
  234. ldap_memfree(csn_val.bv_val);
  235. csn_val.bv_val = NULL;
  236. }
  237. return rc;
  238. }
  239. void
  240. add_repl_control_mods(Slapi_PBlock *pb, Slapi_Mods *smods)
  241. {
  242. struct berval *embvp;
  243. LDAPControl **controls = NULL;
  244. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &controls);
  245. if (slapi_control_present(controls,
  246. LDAP_CONTROL_REPL_MODRDN_EXTRAMODS,
  247. &embvp, NULL)) {
  248. if (embvp != NULL && embvp->bv_len > 0 && embvp->bv_val != NULL) {
  249. /* Parse the extramods stuff */
  250. ber_int_t op;
  251. char *type;
  252. ber_len_t emlen;
  253. ber_tag_t emtag;
  254. char *emlast;
  255. BerElement *ember = ber_init(embvp);
  256. if (ember != NULL) {
  257. for (emtag = ber_first_element(ember, &emlen, &emlast);
  258. emtag != LBER_ERROR && emtag != LBER_END_OF_SEQORSET;
  259. emtag = ber_next_element(ember, &emlen, emlast)) {
  260. struct berval **embvals = NULL;
  261. type = NULL;
  262. if (ber_scanf(ember, "{i{a[V]}}", &op, &type, &embvals) != LBER_ERROR) {
  263. slapi_mods_add_modbvps(smods, op, type, embvals);
  264. /* GGOODREPL I suspect this will cause two sets of lastmods attr values
  265. to end up in the entry. We need to remove the old ones.
  266. */
  267. }
  268. slapi_ch_free_string(&type);
  269. ber_bvecfree(embvals);
  270. }
  271. }
  272. ber_free(ember, 1);
  273. }
  274. }
  275. }