modrdn.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /* Portions Copyright (c) 1990 Regents of the University of Michigan.
  16. * All rights reserved.
  17. */
  18. /* Copyright 1999, Juan C. Gomez, All rights reserved.
  19. * This software is not subject to any license of Silicon Graphics
  20. * Inc. or Purdue University.
  21. *
  22. * Redistribution and use in source and binary forms are permitted
  23. * without restriction or fee of any kind as long as this notice
  24. * is preserved.
  25. */
  26. /* ACKNOWLEDGEMENTS:
  27. * Juan C. Gomez
  28. */
  29. #include "portable.h"
  30. #include <stdio.h>
  31. #include <ac/socket.h>
  32. #include <ac/string.h>
  33. #include <ac/time.h>
  34. #include "ldap-int.h"
  35. /*
  36. * A modify rdn request looks like this:
  37. * ModifyRDNRequest ::= SEQUENCE {
  38. * entry DistinguishedName,
  39. * newrdn RelativeDistinguishedName,
  40. * deleteoldrdn BOOLEAN
  41. * newSuperior [0] DistinguishedName [v3 only]
  42. * }
  43. */
  44. BerElement *
  45. ldap_build_moddn_req(
  46. LDAP *ld,
  47. LDAP_CONST char *dn,
  48. LDAP_CONST char *newrdn,
  49. LDAP_CONST char *newSuperior,
  50. int deleteoldrdn,
  51. LDAPControl **sctrls,
  52. LDAPControl **cctrls,
  53. ber_int_t *msgidp )
  54. {
  55. BerElement *ber;
  56. int rc;
  57. /* create a message to send */
  58. if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
  59. return( NULL );
  60. }
  61. LDAP_NEXT_MSGID( ld, *msgidp );
  62. if( newSuperior != NULL ) {
  63. /* must be version 3 (or greater) */
  64. if ( ld->ld_version < LDAP_VERSION3 ) {
  65. ld->ld_errno = LDAP_NOT_SUPPORTED;
  66. ber_free( ber, 1 );
  67. return( NULL );
  68. }
  69. rc = ber_printf( ber, "{it{ssbtsN}", /* '}' */
  70. *msgidp, LDAP_REQ_MODDN,
  71. dn, newrdn, (ber_int_t) deleteoldrdn,
  72. LDAP_TAG_NEWSUPERIOR, newSuperior );
  73. } else {
  74. rc = ber_printf( ber, "{it{ssbN}", /* '}' */
  75. *msgidp, LDAP_REQ_MODDN,
  76. dn, newrdn, (ber_int_t) deleteoldrdn );
  77. }
  78. if ( rc < 0 ) {
  79. ld->ld_errno = LDAP_ENCODING_ERROR;
  80. ber_free( ber, 1 );
  81. return( NULL );
  82. }
  83. /* Put Server Controls */
  84. if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
  85. ber_free( ber, 1 );
  86. return( NULL );
  87. }
  88. rc = ber_printf( ber, /*{*/ "N}" );
  89. if ( rc < 0 ) {
  90. ld->ld_errno = LDAP_ENCODING_ERROR;
  91. ber_free( ber, 1 );
  92. return( NULL );
  93. }
  94. return( ber );
  95. }
  96. /*
  97. * ldap_rename - initiate an ldap extended modifyDN operation.
  98. *
  99. * Parameters:
  100. * ld LDAP descriptor
  101. * dn DN of the object to modify
  102. * newrdn RDN to give the object
  103. * deleteoldrdn nonzero means to delete old rdn values from the entry
  104. * newSuperior DN of the new parent if applicable
  105. *
  106. * Returns the LDAP error code.
  107. */
  108. int
  109. ldap_rename(
  110. LDAP *ld,
  111. LDAP_CONST char *dn,
  112. LDAP_CONST char *newrdn,
  113. LDAP_CONST char *newSuperior,
  114. int deleteoldrdn,
  115. LDAPControl **sctrls,
  116. LDAPControl **cctrls,
  117. int *msgidp )
  118. {
  119. BerElement *ber;
  120. int rc;
  121. ber_int_t id;
  122. Debug0( LDAP_DEBUG_TRACE, "ldap_rename\n" );
  123. /* check client controls */
  124. rc = ldap_int_client_controls( ld, cctrls );
  125. if( rc != LDAP_SUCCESS ) return rc;
  126. ber = ldap_build_moddn_req( ld, dn, newrdn, newSuperior,
  127. deleteoldrdn, sctrls, cctrls, &id );
  128. if( !ber )
  129. return ld->ld_errno;
  130. /* send the message */
  131. *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODRDN, dn, ber, id );
  132. if( *msgidp < 0 ) {
  133. return( ld->ld_errno );
  134. }
  135. return LDAP_SUCCESS;
  136. }
  137. /*
  138. * ldap_rename2 - initiate an ldap (and X.500) modifyDN operation. Parameters:
  139. * (LDAP V3 MODIFYDN REQUEST)
  140. * ld LDAP descriptor
  141. * dn DN of the object to modify
  142. * newrdn RDN to give the object
  143. * deleteoldrdn nonzero means to delete old rdn values from the entry
  144. * newSuperior DN of the new parent if applicable
  145. *
  146. * ldap_rename2 uses a U-Mich Style API. It returns the msgid.
  147. */
  148. int
  149. ldap_rename2(
  150. LDAP *ld,
  151. LDAP_CONST char *dn,
  152. LDAP_CONST char *newrdn,
  153. LDAP_CONST char *newSuperior,
  154. int deleteoldrdn )
  155. {
  156. int msgid;
  157. int rc;
  158. Debug0( LDAP_DEBUG_TRACE, "ldap_rename2\n" );
  159. rc = ldap_rename( ld, dn, newrdn, newSuperior,
  160. deleteoldrdn, NULL, NULL, &msgid );
  161. return rc == LDAP_SUCCESS ? msgid : -1;
  162. }
  163. /*
  164. * ldap_modrdn2 - initiate an ldap modifyRDN operation. Parameters:
  165. *
  166. * ld LDAP descriptor
  167. * dn DN of the object to modify
  168. * newrdn RDN to give the object
  169. * deleteoldrdn nonzero means to delete old rdn values from the entry
  170. *
  171. * Example:
  172. * msgid = ldap_modrdn( ld, dn, newrdn );
  173. */
  174. int
  175. ldap_modrdn2( LDAP *ld,
  176. LDAP_CONST char *dn,
  177. LDAP_CONST char *newrdn,
  178. int deleteoldrdn )
  179. {
  180. return ldap_rename2( ld, dn, newrdn, NULL, deleteoldrdn );
  181. }
  182. int
  183. ldap_modrdn( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn )
  184. {
  185. return( ldap_rename2( ld, dn, newrdn, NULL, 1 ) );
  186. }
  187. int
  188. ldap_rename_s(
  189. LDAP *ld,
  190. LDAP_CONST char *dn,
  191. LDAP_CONST char *newrdn,
  192. LDAP_CONST char *newSuperior,
  193. int deleteoldrdn,
  194. LDAPControl **sctrls,
  195. LDAPControl **cctrls )
  196. {
  197. int rc;
  198. int msgid;
  199. LDAPMessage *res;
  200. rc = ldap_rename( ld, dn, newrdn, newSuperior,
  201. deleteoldrdn, sctrls, cctrls, &msgid );
  202. if( rc != LDAP_SUCCESS ) {
  203. return rc;
  204. }
  205. rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &res );
  206. if( rc == -1 || !res ) {
  207. return ld->ld_errno;
  208. }
  209. return ldap_result2error( ld, res, 1 );
  210. }
  211. int
  212. ldap_rename2_s(
  213. LDAP *ld,
  214. LDAP_CONST char *dn,
  215. LDAP_CONST char *newrdn,
  216. LDAP_CONST char *newSuperior,
  217. int deleteoldrdn )
  218. {
  219. return ldap_rename_s( ld, dn, newrdn, newSuperior,
  220. deleteoldrdn, NULL, NULL );
  221. }
  222. int
  223. ldap_modrdn2_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn, int deleteoldrdn )
  224. {
  225. return ldap_rename_s( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL );
  226. }
  227. int
  228. ldap_modrdn_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn )
  229. {
  230. return ldap_rename_s( ld, dn, newrdn, NULL, 1, NULL, NULL );
  231. }