repl_controls.c 7.9 KB

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