repl_controls.c 9.9 KB

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