modify.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "portable.h"
  19. #include <stdio.h>
  20. #include <ac/socket.h>
  21. #include <ac/string.h>
  22. #include <ac/time.h>
  23. #include "ldap-int.h"
  24. /* A modify request/response looks like this:
  25. * ModifyRequest ::= [APPLICATION 6] SEQUENCE {
  26. * object LDAPDN,
  27. * changes SEQUENCE OF change SEQUENCE {
  28. * operation ENUMERATED {
  29. * add (0),
  30. * delete (1),
  31. * replace (2),
  32. * ... },
  33. * modification PartialAttribute } }
  34. *
  35. * PartialAttribute ::= SEQUENCE {
  36. * type AttributeDescription,
  37. * vals SET OF value AttributeValue }
  38. *
  39. * AttributeDescription ::= LDAPString
  40. * -- Constrained to <attributedescription> [RFC4512]
  41. *
  42. * AttributeValue ::= OCTET STRING
  43. *
  44. * ModifyResponse ::= [APPLICATION 7] LDAPResult
  45. *
  46. * (Source: RFC 4511)
  47. */
  48. BerElement *
  49. ldap_build_modify_req(
  50. LDAP *ld,
  51. LDAP_CONST char *dn,
  52. LDAPMod **mods,
  53. LDAPControl **sctrls,
  54. LDAPControl **cctrls,
  55. ber_int_t *msgidp )
  56. {
  57. BerElement *ber;
  58. int i, rc;
  59. /* create a message to send */
  60. if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
  61. return( NULL );
  62. }
  63. LDAP_NEXT_MSGID( ld, *msgidp );
  64. rc = ber_printf( ber, "{it{s{" /*}}}*/, *msgidp, LDAP_REQ_MODIFY, dn );
  65. if ( rc == -1 ) {
  66. ld->ld_errno = LDAP_ENCODING_ERROR;
  67. ber_free( ber, 1 );
  68. return( NULL );
  69. }
  70. /* allow mods to be NULL ("touch") */
  71. if ( mods ) {
  72. /* for each modification to be performed... */
  73. for ( i = 0; mods[i] != NULL; i++ ) {
  74. if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
  75. rc = ber_printf( ber, "{e{s[V]N}N}",
  76. (ber_int_t) ( mods[i]->mod_op & ~LDAP_MOD_BVALUES ),
  77. mods[i]->mod_type, mods[i]->mod_bvalues );
  78. } else {
  79. rc = ber_printf( ber, "{e{s[v]N}N}",
  80. (ber_int_t) mods[i]->mod_op,
  81. mods[i]->mod_type, mods[i]->mod_values );
  82. }
  83. if ( rc == -1 ) {
  84. ld->ld_errno = LDAP_ENCODING_ERROR;
  85. ber_free( ber, 1 );
  86. return( NULL );
  87. }
  88. }
  89. }
  90. if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
  91. ld->ld_errno = LDAP_ENCODING_ERROR;
  92. ber_free( ber, 1 );
  93. return( NULL );
  94. }
  95. /* Put Server Controls */
  96. if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
  97. ber_free( ber, 1 );
  98. return( NULL );
  99. }
  100. if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
  101. ld->ld_errno = LDAP_ENCODING_ERROR;
  102. ber_free( ber, 1 );
  103. return( NULL );
  104. }
  105. return( ber );
  106. }
  107. /*
  108. * ldap_modify_ext - initiate an ldap extended modify operation.
  109. *
  110. * Parameters:
  111. *
  112. * ld LDAP descriptor
  113. * dn DN of the object to modify
  114. * mods List of modifications to make. This is null-terminated
  115. * array of struct ldapmod's, specifying the modifications
  116. * to perform.
  117. * sctrls Server Controls
  118. * cctrls Client Controls
  119. * msgidp Message ID pointer
  120. *
  121. * Example:
  122. * LDAPMod *mods[] = {
  123. * { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
  124. * { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },
  125. * { LDAP_MOD_DELETE, "ou", 0 },
  126. * { LDAP_MOD_INCREMENT, "uidNumber, { "1", 0 } }
  127. * 0
  128. * }
  129. * rc= ldap_modify_ext( ld, dn, mods, sctrls, cctrls, &msgid );
  130. */
  131. int
  132. ldap_modify_ext( LDAP *ld,
  133. LDAP_CONST char *dn,
  134. LDAPMod **mods,
  135. LDAPControl **sctrls,
  136. LDAPControl **cctrls,
  137. int *msgidp )
  138. {
  139. BerElement *ber;
  140. int rc;
  141. ber_int_t id;
  142. Debug0( LDAP_DEBUG_TRACE, "ldap_modify_ext\n" );
  143. /* check client controls */
  144. rc = ldap_int_client_controls( ld, cctrls );
  145. if( rc != LDAP_SUCCESS ) return rc;
  146. ber = ldap_build_modify_req( ld, dn, mods, sctrls, cctrls, &id );
  147. if( !ber )
  148. return ld->ld_errno;
  149. /* send the message */
  150. *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber, id );
  151. return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
  152. }
  153. /*
  154. * ldap_modify - initiate an ldap modify operation.
  155. *
  156. * Parameters:
  157. *
  158. * ld LDAP descriptor
  159. * dn DN of the object to modify
  160. * mods List of modifications to make. This is null-terminated
  161. * array of struct ldapmod's, specifying the modifications
  162. * to perform.
  163. *
  164. * Example:
  165. * LDAPMod *mods[] = {
  166. * { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
  167. * { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },
  168. * { LDAP_MOD_DELETE, "ou", 0 },
  169. * { LDAP_MOD_INCREMENT, "uidNumber, { "1", 0 } }
  170. * 0
  171. * }
  172. * msgid = ldap_modify( ld, dn, mods );
  173. */
  174. int
  175. ldap_modify( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )
  176. {
  177. int rc, msgid;
  178. Debug0( LDAP_DEBUG_TRACE, "ldap_modify\n" );
  179. rc = ldap_modify_ext( ld, dn, mods, NULL, NULL, &msgid );
  180. if ( rc != LDAP_SUCCESS )
  181. return -1;
  182. return msgid;
  183. }
  184. int
  185. ldap_modify_ext_s( LDAP *ld, LDAP_CONST char *dn,
  186. LDAPMod **mods, LDAPControl **sctrl, LDAPControl **cctrl )
  187. {
  188. int rc;
  189. int msgid;
  190. LDAPMessage *res;
  191. rc = ldap_modify_ext( ld, dn, mods, sctrl, cctrl, &msgid );
  192. if ( rc != LDAP_SUCCESS )
  193. return( rc );
  194. if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
  195. return( ld->ld_errno );
  196. return( ldap_result2error( ld, res, 1 ) );
  197. }
  198. int
  199. ldap_modify_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )
  200. {
  201. return ldap_modify_ext_s( ld, dn, mods, NULL, NULL );
  202. }