repl_controls.c 7.9 KB

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