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 ))
107 unsigned long tag, len;
110 assert( ber != NULL );
111 assert( ctrls != NULL );
115 len = ber->ber_end - ber->ber_ptr;
122 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
123 if( tag == LBER_ERROR ) {
125 return LDAP_DECODING_ERROR;
128 /* ignore unexpected input */
132 /* set through each element */
134 *ctrls = malloc( 1 * sizeof(LDAPControl *) );
136 if( *ctrls == NULL ) {
137 return LDAP_NO_MEMORY;
140 ctrls[nctrls] = NULL;
142 for( tag = ber_first_element( ber, &len, &opaque );
145 #ifdef LDAP_END_SEQORSET
146 && tag != LBER_END_OF_SEQORSET
149 tag = ber_next_element( ber, &len, opaque ) )
152 LDAPControl **tctrls;
154 tctrl = calloc( 1, sizeof(LDAPControl) );
156 /* allocate pointer space for current controls (nctrls)
157 * + this control + extra NULL
159 tctrls = (tctrl == NULL) ? NULL :
160 realloc(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
162 if( tctrls == NULL ) {
163 /* one of the above allocation failed */
165 if( tctrl != NULL ) {
169 ldap_controls_free(*ctrls);
172 return LDAP_NO_MEMORY;
176 tctrls[nctrls++] = tctrl;
177 tctrls[nctrls] = NULL;
179 tag = ber_scanf( ber, "{a", &tctrl->ldctl_oid );
181 if( tag != LBER_ERROR ) {
182 tag = ber_peek_tag( ber, &len );
185 if( tag == LBER_BOOLEAN ) {
186 tag = ber_scanf( ber, "b", &tctrl->ldctl_iscritical );
189 if( tag != LBER_ERROR ) {
190 tag = ber_peek_tag( ber, &len );
193 if( tag == LBER_OCTETSTRING ) {
194 tag = ber_scanf( ber, "o", &tctrl->ldctl_value );
197 tctrl->ldctl_value.bv_val = NULL;
200 if( tag == LBER_ERROR ) {
202 ldap_controls_free( tctrls );
203 return LDAP_DECODING_ERROR;
216 ldap_control_free( LDAPControl *c )
219 if( c->ldctl_oid != NULL) {
220 free( c->ldctl_oid );
223 if( c->ldctl_value.bv_val != NULL ) {
224 free( c->ldctl_value.bv_val );
232 * Free an array of LDAPControl's
235 ldap_controls_free( LDAPControl **controls )
237 if ( controls != NULL ) {
240 for(c = *controls; c != NULL; c++) {
241 ldap_control_free( c );
249 * Duplicate an array of LDAPControl
251 LDAPControl **ldap_controls_dup( const LDAPControl **controls )
256 if ( controls == NULL ) {
260 /* count the controls */
261 for(i=0; controls[i] != NULL; i++) /* empty */ ;
264 /* no controls to duplicate */
268 new = (LDAPControl **) malloc( i * sizeof(LDAPControl *) );
271 /* memory allocation failure */
275 /* duplicate the controls */
276 for(i=0; controls[i] != NULL; i++) {
277 new[i] = ldap_control_dup( controls[i] );
279 if( new[i] == NULL ) {
280 ldap_controls_free( new );
291 * Duplicate a LDAPControl
293 LDAPControl *ldap_control_dup( const LDAPControl *c )
301 new = (LDAPControl *) malloc( sizeof(LDAPControl) );
307 if( c->ldctl_oid != NULL ) {
308 new->ldctl_oid = strdup( c->ldctl_oid );
310 if(new->ldctl_oid == NULL) {
316 new->ldctl_oid = NULL;
319 if( c->ldctl_value.bv_len > 0 ) {
320 new->ldctl_value.bv_val = (char *) malloc( c->ldctl_value.bv_len );
322 if(new->ldctl_value.bv_val == NULL) {
323 if(new->ldctl_oid != NULL) {
324 free( new->ldctl_oid );
330 SAFEMEMCPY( new->ldctl_value.bv_val, c->ldctl_value.bv_val,
331 c->ldctl_value.bv_len );
333 new->ldctl_value.bv_len = c->ldctl_value.bv_len;
336 new->ldctl_value.bv_len = 0;
337 new->ldctl_value.bv_val = NULL;
340 new->ldctl_iscritical = c->ldctl_iscritical;