2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
14 #include <ac/string.h>
20 * ldap_int_put_controls
23 int ldap_int_put_controls(
31 assert( ber != NULL );
34 /* use default server controls */
35 ctrls = ld->ld_sctrls;
38 if( ctrls == NULL || *ctrls == NULL ) {
42 if ( ld->ld_version < LDAP_VERSION3 ) {
43 /* LDAPv2 doesn't support controls,
44 * error if any control is critical
46 for( c = ctrls ; *c != NULL; c++ ) {
47 if( (*c)->ldctl_iscritical ) {
48 ld->ld_errno = LDAP_NOT_SUPPORTED;
56 /* Controls are encoded as a sequence of sequences */
57 if( ber_printf( ber, "t{", LDAP_TAG_CONTROLS ) == -1 ) {
58 ld->ld_errno = LDAP_ENCODING_ERROR;
62 for( c = ctrls ; *c != NULL; c++ ) {
63 if ( ber_printf( ber, "{s",
64 (*c)->ldctl_oid ) == -1 )
66 ld->ld_errno = LDAP_ENCODING_ERROR;
70 if( (*c)->ldctl_iscritical /* only if true */
71 && ( ber_printf( ber, "b",
72 (*c)->ldctl_iscritical ) == -1 ) )
74 ld->ld_errno = LDAP_ENCODING_ERROR;
78 if( (*c)->ldctl_value.bv_val != NULL /* only if we have a value */
79 && ( ber_printf( ber, "O",
80 &((*c)->ldctl_value) ) == -1 ) )
82 ld->ld_errno = LDAP_ENCODING_ERROR;
87 if( ber_printf( ber, "}" ) == -1 ) {
88 ld->ld_errno = LDAP_ENCODING_ERROR;
94 if( ber_printf( ber, "}" ) == -1 ) {
95 ld->ld_errno = LDAP_ENCODING_ERROR;
102 int ldap_int_get_controls LDAP_P((
104 LDAPControl ***ctrls ))
106 assert( be != NULL );
107 assert( ctrls != NULL );
111 return LDAP_NOT_SUPPORTED;
118 ldap_control_free( LDAPControl *c )
121 if( c->ldctl_oid != NULL) {
122 free( c->ldctl_oid );
125 if( c->ldctl_value.bv_val != NULL ) {
126 free( c->ldctl_value.bv_val );
134 * Free an array of LDAPControl's
137 ldap_controls_free( LDAPControl **controls )
139 if ( controls != NULL ) {
142 for(c = *controls; c != NULL; c++) {
143 ldap_control_free( c );
151 * Duplicate an array of LDAPControl
153 LDAPControl **ldap_controls_dup( const LDAPControl **controls )
158 if ( controls == NULL ) {
162 /* count the controls */
163 for(i=0; controls[i] != NULL; i++) /* empty */ ;
166 /* no controls to duplicate */
170 new = (LDAPControl **) malloc( i * sizeof(LDAPControl *) );
173 /* memory allocation failure */
177 /* duplicate the controls */
178 for(i=0; controls[i] != NULL; i++) {
179 new[i] = ldap_control_dup( controls[i] );
181 if( new[i] == NULL ) {
182 ldap_controls_free( new );
193 * Duplicate a LDAPControl
195 LDAPControl *ldap_control_dup( const LDAPControl *c )
203 new = (LDAPControl *) malloc( sizeof(LDAPControl) );
209 if( c->ldctl_oid != NULL ) {
210 new->ldctl_oid = strdup( c->ldctl_oid );
212 if(new->ldctl_oid == NULL) {
218 new->ldctl_oid = NULL;
221 if( c->ldctl_value.bv_len > 0 ) {
222 new->ldctl_value.bv_val = (char *) malloc( c->ldctl_value.bv_len );
224 if(new->ldctl_value.bv_val == NULL) {
225 if(new->ldctl_oid != NULL) {
226 free( new->ldctl_oid );
232 SAFEMEMCPY( new->ldctl_value.bv_val, c->ldctl_value.bv_val,
233 c->ldctl_value.bv_len );
235 new->ldctl_value.bv_len = c->ldctl_value.bv_len;
238 new->ldctl_value.bv_len = 0;
239 new->ldctl_value.bv_val = NULL;
242 new->ldctl_iscritical = c->ldctl_iscritical;