repl_controls.c 8.1 KB

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