repl_controls.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "slapi-plugin.h"
  42. #include "repl5.h"
  43. #include "repl.h" /* For LDAP_CONTROL_REPL_MODRDN_EXTRAMODS */
  44. /*
  45. * repl_controls.c - convenience functions for creating and
  46. * decoding controls that implement 5.0-style replication
  47. * protocol operations.
  48. *
  49. * TODO: Send modrdn mods with modrdn operation
  50. * Fix ber_printf() and ber_scanf() format strings - some are
  51. * the wrong types.
  52. */
  53. /*
  54. * Return a pointer to a NSDS50ReplUpdateInfoControl.
  55. * The control looks like this:
  56. *
  57. * NSDS50ReplUpdateInfoControl ::= SEQUENCE {
  58. * uuid OCTET STRING,
  59. * csn OCTET STRING,
  60. * OPTIONAL [new]superior-uuid OCTET STRING
  61. * OPTIONAL modrdn_mods XXXggood WHAT TYPE???
  62. * }
  63. */
  64. int
  65. create_NSDS50ReplUpdateInfoControl(const char *uuid,
  66. const char *superior_uuid, const CSN *csn,
  67. LDAPMod **modrdn_mods, LDAPControl **ctrlp)
  68. {
  69. int retval;
  70. BerElement *tmp_bere = NULL;
  71. char csn_str[CSN_STRSIZE];
  72. if (NULL == ctrlp)
  73. {
  74. retval = LDAP_PARAM_ERROR;
  75. goto loser;
  76. }
  77. else
  78. {
  79. if ((tmp_bere = ber_alloc()) == NULL)
  80. {
  81. retval = LDAP_NO_MEMORY;
  82. goto loser;
  83. }
  84. else
  85. {
  86. /* Stuff uuid and csn into BerElement */
  87. if (ber_printf(tmp_bere, "{") == -1)
  88. {
  89. retval = LDAP_ENCODING_ERROR;
  90. goto loser;
  91. }
  92. /* Stuff uuid of this entry into BerElement */
  93. if (ber_printf(tmp_bere, "s", uuid) == -1)
  94. {
  95. retval = LDAP_ENCODING_ERROR;
  96. goto loser;
  97. }
  98. /* Stuff csn of this change into BerElement */
  99. csn_as_string(csn, PR_FALSE, csn_str);
  100. if (ber_printf(tmp_bere, "s", csn_str) == -1)
  101. {
  102. retval = LDAP_ENCODING_ERROR;
  103. goto loser;
  104. }
  105. /* If present, stuff uuid of parent entry into BerElement */
  106. if (NULL != superior_uuid)
  107. {
  108. if (ber_printf(tmp_bere, "s", superior_uuid) == -1)
  109. {
  110. retval = LDAP_ENCODING_ERROR;
  111. goto loser;
  112. }
  113. }
  114. /* If present, add the modrdn mods */
  115. if (NULL != modrdn_mods)
  116. {
  117. int i;
  118. if (ber_printf(tmp_bere, "{" ) == -1)
  119. {
  120. retval = LDAP_ENCODING_ERROR;
  121. goto loser;
  122. }
  123. /* for each modification to be performed... */
  124. for (i = 0; NULL != modrdn_mods[i]; i++)
  125. {
  126. if (ber_printf(tmp_bere, "{e{s[V]}}",
  127. modrdn_mods[i]->mod_op & ~LDAP_MOD_BVALUES,
  128. modrdn_mods[i]->mod_type, modrdn_mods[i]->mod_bvalues ) == -1)
  129. {
  130. retval = LDAP_ENCODING_ERROR;
  131. goto loser;
  132. }
  133. }
  134. if (ber_printf(tmp_bere, "}") == -1)
  135. {
  136. retval = LDAP_ENCODING_ERROR;
  137. goto loser;
  138. }
  139. }
  140. /* Close the sequence */
  141. if (ber_printf(tmp_bere, "}") == -1)
  142. {
  143. retval = LDAP_ENCODING_ERROR;
  144. goto loser;
  145. }
  146. retval = slapi_build_control( REPL_NSDS50_UPDATE_INFO_CONTROL_OID,
  147. tmp_bere, 1 /* is critical */, ctrlp);
  148. }
  149. }
  150. loser:
  151. if (NULL != tmp_bere)
  152. {
  153. ber_free(tmp_bere, 1);
  154. tmp_bere = NULL;
  155. }
  156. return retval;
  157. }
  158. /*
  159. * Destroy a ReplUpdateInfoControl and set the pointer to NULL.
  160. */
  161. void
  162. destroy_NSDS50ReplUpdateInfoControl(LDAPControl **ctrlp)
  163. {
  164. if (NULL != ctrlp && NULL != *ctrlp)
  165. {
  166. ldap_control_free(*ctrlp);
  167. *ctrlp = NULL;
  168. }
  169. }
  170. /*
  171. * Look through the array of controls. If an NSDS50ReplUpdateInfoControl
  172. * is present, decode it and return pointers to the broken-out
  173. * components. The caller is responsible for freeing pointers to
  174. * the returned objects. The caller may indicate that it is not
  175. * interested in any of the output parameters by passing NULL
  176. * for that parameter.
  177. *
  178. * Returns 0 if the control is not present, 1 if it is present, and
  179. * -1 if an error occurs.
  180. */
  181. int
  182. decode_NSDS50ReplUpdateInfoControl(LDAPControl **controlsp,
  183. char **uuid, char **superior_uuid,
  184. CSN **csn, LDAPMod ***modrdn_mods)
  185. {
  186. struct berval *ctl_value = NULL;
  187. int iscritical = 0;
  188. int rc = -1;
  189. struct berval uuid_val = {0};
  190. struct berval superior_uuid_val = {0};
  191. struct berval csn_val = {0};
  192. BerElement *tmp_bere = NULL;
  193. Slapi_Mods modrdn_smods;
  194. PRBool got_modrdn_mods = PR_FALSE;
  195. ber_len_t len;
  196. slapi_mods_init(&modrdn_smods, 4);
  197. if (slapi_control_present(controlsp, REPL_NSDS50_UPDATE_INFO_CONTROL_OID,
  198. &ctl_value, &iscritical))
  199. {
  200. if (!BV_HAS_DATA(ctl_value) || (tmp_bere = ber_init(ctl_value)) == NULL)
  201. {
  202. rc = -1;
  203. goto loser;
  204. }
  205. if (ber_scanf(tmp_bere, "{oo", &uuid_val, &csn_val) == LBER_ERROR)
  206. {
  207. rc = -1;
  208. goto loser;
  209. }
  210. if (ber_peek_tag(tmp_bere, &len) == LBER_OCTETSTRING)
  211. {
  212. /* The optional superior_uuid is present */
  213. if (ber_scanf(tmp_bere, "o", &superior_uuid_val) == LBER_DEFAULT)
  214. {
  215. rc = -1;
  216. goto loser;
  217. }
  218. }
  219. if (ber_peek_tag(tmp_bere, &len) == LBER_SEQUENCE)
  220. {
  221. ber_tag_t emtag;
  222. ber_len_t emlen;
  223. char *emlast;
  224. for ( emtag = ber_first_element( tmp_bere, &emlen, &emlast );
  225. emtag != LBER_ERROR && emtag != LBER_END_OF_SEQORSET;
  226. emtag = ber_next_element( tmp_bere, &emlen, emlast ))
  227. {
  228. struct berval **embvals;
  229. ber_int_t op;
  230. char *type;
  231. if ( ber_scanf( tmp_bere, "{i{a[V]}}", &op, &type, &embvals ) == LBER_ERROR )
  232. {
  233. rc = -1;
  234. goto loser;
  235. }
  236. slapi_mods_add_modbvps(&modrdn_smods, op, type, embvals);
  237. slapi_ch_free_string( &type );
  238. ber_bvecfree( embvals );
  239. }
  240. got_modrdn_mods = PR_TRUE;
  241. }
  242. if (ber_scanf(tmp_bere, "}") == LBER_ERROR)
  243. {
  244. rc = -1;
  245. goto loser;
  246. }
  247. if (NULL != uuid)
  248. {
  249. *uuid = slapi_ch_malloc(uuid_val.bv_len + 1);
  250. strncpy(*uuid, uuid_val.bv_val, uuid_val.bv_len);
  251. (*uuid)[uuid_val.bv_len] = '\0';
  252. }
  253. if (NULL != csn)
  254. {
  255. char *csnstr = slapi_ch_malloc(csn_val.bv_len + 1);
  256. strncpy(csnstr, csn_val.bv_val, csn_val.bv_len);
  257. csnstr[csn_val.bv_len] = '\0';
  258. *csn = csn_new_by_string(csnstr);
  259. slapi_ch_free((void **)&csnstr);
  260. }
  261. if (NULL != superior_uuid && NULL != superior_uuid_val.bv_val)
  262. {
  263. *superior_uuid = slapi_ch_malloc(superior_uuid_val.bv_len + 1);
  264. strncpy(*superior_uuid, superior_uuid_val.bv_val,
  265. superior_uuid_val.bv_len);
  266. (*superior_uuid)[superior_uuid_val.bv_len] = '\0';
  267. }
  268. if (NULL != modrdn_mods && got_modrdn_mods)
  269. {
  270. *modrdn_mods = slapi_mods_get_ldapmods_passout(&modrdn_smods);
  271. }
  272. slapi_mods_done(&modrdn_smods);
  273. rc = 1;
  274. }
  275. else
  276. {
  277. rc = 0;
  278. }
  279. loser:
  280. /* XXXggood free CSN here if allocated */
  281. if (NULL != tmp_bere)
  282. {
  283. ber_free(tmp_bere, 1);
  284. tmp_bere = NULL;
  285. }
  286. if (NULL != uuid_val.bv_val)
  287. {
  288. ldap_memfree(uuid_val.bv_val);
  289. uuid_val.bv_val = NULL;
  290. }
  291. if (NULL != superior_uuid_val.bv_val)
  292. {
  293. ldap_memfree(superior_uuid_val.bv_val);
  294. superior_uuid_val.bv_val = NULL;
  295. }
  296. if (NULL != csn_val.bv_val)
  297. {
  298. ldap_memfree(csn_val.bv_val);
  299. csn_val.bv_val = NULL;
  300. }
  301. return rc;
  302. }
  303. void
  304. add_repl_control_mods( Slapi_PBlock *pb, Slapi_Mods *smods )
  305. {
  306. struct berval *embvp;
  307. LDAPControl **controls = NULL;
  308. slapi_pblock_get( pb, SLAPI_REQCONTROLS, &controls);
  309. if ( slapi_control_present( controls,
  310. LDAP_CONTROL_REPL_MODRDN_EXTRAMODS,
  311. &embvp, NULL ))
  312. {
  313. if ( embvp != NULL && embvp->bv_len > 0 && embvp->bv_val != NULL )
  314. {
  315. /* Parse the extramods stuff */
  316. ber_int_t op;
  317. char *type;
  318. ber_len_t emlen;
  319. ber_tag_t emtag;
  320. char *emlast;
  321. BerElement *ember = ber_init( embvp );
  322. if ( ember != NULL )
  323. {
  324. for ( emtag = ber_first_element( ember, &emlen, &emlast );
  325. emtag != LBER_ERROR && emtag != LBER_END_OF_SEQORSET;
  326. emtag = ber_next_element( ember, &emlen, emlast ))
  327. {
  328. struct berval **embvals = NULL;
  329. type = NULL;
  330. if ( ber_scanf( ember, "{i{a[V]}}", &op, &type, &embvals ) != LBER_ERROR )
  331. {
  332. slapi_mods_add_modbvps( smods, op, type, embvals);
  333. /* GGOODREPL I suspect this will cause two sets of lastmods attr values
  334. to end up in the entry. We need to remove the old ones.
  335. */
  336. }
  337. slapi_ch_free_string( &type );
  338. ber_bvecfree( embvals );
  339. }
  340. }
  341. ber_free( ember, 1 );
  342. }
  343. }
  344. }