2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* LDAPv3 Controls (RFC2251)
8 * Controls ::= SEQUENCE OF Control
10 * Control ::= SEQUENCE {
11 * controlType LDAPOID,
12 * criticality BOOLEAN DEFAULT FALSE,
13 * controlValue OCTET STRING OPTIONAL
19 #include <ac/stdlib.h>
22 #include <ac/string.h>
28 * ldap_int_put_controls
31 int ldap_int_put_controls(
39 assert( ber != NULL );
42 /* use default server controls */
43 ctrls = ld->ld_sctrls;
46 if( ctrls == NULL || *ctrls == NULL ) {
50 if ( ld->ld_version < LDAP_VERSION3 ) {
51 /* LDAPv2 doesn't support controls,
52 * error if any control is critical
54 for( c = ctrls ; *c != NULL; c++ ) {
55 if( (*c)->ldctl_iscritical ) {
56 ld->ld_errno = LDAP_NOT_SUPPORTED;
64 /* Controls are encoded as a sequence of sequences */
65 if( ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS ) == -1 ) {
66 ld->ld_errno = LDAP_ENCODING_ERROR;
70 for( c = ctrls ; *c != NULL; c++ ) {
71 if ( ber_printf( ber, "{s" /*}*/,
72 (*c)->ldctl_oid ) == -1 )
74 ld->ld_errno = LDAP_ENCODING_ERROR;
78 if( (*c)->ldctl_iscritical /* only if true */
79 && ( ber_printf( ber, "b",
80 (ber_int_t) (*c)->ldctl_iscritical ) == -1 ) )
82 ld->ld_errno = LDAP_ENCODING_ERROR;
86 if( (*c)->ldctl_value.bv_val != NULL /* only if we have a value */
87 && ( ber_printf( ber, "O",
88 &((*c)->ldctl_value) ) == -1 ) )
90 ld->ld_errno = LDAP_ENCODING_ERROR;
95 if( ber_printf( ber, /*{*/"}" ) == -1 ) {
96 ld->ld_errno = LDAP_ENCODING_ERROR;
102 if( ber_printf( ber, /*{*/"}" ) == -1 ) {
103 ld->ld_errno = LDAP_ENCODING_ERROR;
110 int ldap_int_get_controls(
112 LDAPControl ***ctrls )
119 assert( ber != NULL );
120 assert( ctrls != NULL );
124 len = ber_pvt_ber_remaining(ber);
131 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
132 if( tag == LBER_ERROR ) {
134 return LDAP_DECODING_ERROR;
137 /* ignore unexpected input */
141 /* set through each element */
143 *ctrls = LDAP_MALLOC( 1 * sizeof(LDAPControl *) );
145 if( *ctrls == NULL ) {
146 return LDAP_NO_MEMORY;
149 ctrls[nctrls] = NULL;
151 for( tag = ber_first_element( ber, &len, &opaque );
153 tag = ber_next_element( ber, &len, opaque ) )
156 LDAPControl **tctrls;
158 tctrl = LDAP_CALLOC( 1, sizeof(LDAPControl) );
160 /* allocate pointer space for current controls (nctrls)
161 * + this control + extra NULL
163 tctrls = (tctrl == NULL) ? NULL :
164 LDAP_REALLOC(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
166 if( tctrls == NULL ) {
167 /* one of the above allocation failed */
169 if( tctrl != NULL ) {
173 ldap_controls_free(*ctrls);
176 return LDAP_NO_MEMORY;
180 tctrls[nctrls++] = tctrl;
181 tctrls[nctrls] = NULL;
183 tag = ber_scanf( ber, "{a" /*}*/, &tctrl->ldctl_oid );
185 if( tag != LBER_ERROR ) {
186 tag = ber_peek_tag( ber, &len );
189 if( tag == LBER_BOOLEAN ) {
191 tag = ber_scanf( ber, "b", &crit );
192 tctrl->ldctl_iscritical = crit ? (char) 0 : (char) ~0;
195 if( tag != LBER_ERROR ) {
196 tag = ber_peek_tag( ber, &len );
199 if( tag == LBER_OCTETSTRING ) {
200 tag = ber_scanf( ber, "o", &tctrl->ldctl_value );
203 tctrl->ldctl_value.bv_val = NULL;
206 if( tag == LBER_ERROR ) {
208 ldap_controls_free( tctrls );
209 return LDAP_DECODING_ERROR;
222 ldap_control_free( LDAPControl *c )
227 if( c->ldctl_oid != NULL) {
228 LDAP_FREE( c->ldctl_oid );
231 if( c->ldctl_value.bv_val != NULL ) {
232 LDAP_FREE( c->ldctl_value.bv_val );
240 * Free an array of LDAPControl's
243 ldap_controls_free( LDAPControl **controls )
245 assert( controls != NULL );
247 if ( controls != NULL ) {
250 for(c = *controls; c != NULL; c++) {
251 ldap_control_free( c );
254 LDAP_FREE( controls );
259 * Duplicate an array of LDAPControl
261 LDAPControl **ldap_controls_dup( LDAPControl **controls )
266 if ( controls == NULL ) {
270 /* count the controls */
271 for(i=0; controls[i] != NULL; i++) /* empty */ ;
274 /* no controls to duplicate */
278 new = (LDAPControl **) LDAP_MALLOC( i * sizeof(LDAPControl *) );
281 /* memory allocation failure */
285 /* duplicate the controls */
286 for(i=0; controls[i] != NULL; i++) {
287 new[i] = ldap_control_dup( controls[i] );
289 if( new[i] == NULL ) {
290 ldap_controls_free( new );
301 * Duplicate a LDAPControl
303 LDAPControl *ldap_control_dup( LDAPControl *c )
311 new = (LDAPControl *) LDAP_MALLOC( sizeof(LDAPControl) );
317 if( c->ldctl_oid != NULL ) {
318 new->ldctl_oid = LDAP_STRDUP( c->ldctl_oid );
320 if(new->ldctl_oid == NULL) {
326 new->ldctl_oid = NULL;
329 if( c->ldctl_value.bv_len > 0 ) {
330 new->ldctl_value.bv_val = (char *) LDAP_MALLOC( c->ldctl_value.bv_len );
332 if(new->ldctl_value.bv_val == NULL) {
333 if(new->ldctl_oid != NULL) {
334 LDAP_FREE( new->ldctl_oid );
340 SAFEMEMCPY( new->ldctl_value.bv_val, c->ldctl_value.bv_val,
341 c->ldctl_value.bv_len );
343 new->ldctl_value.bv_len = c->ldctl_value.bv_len;
346 new->ldctl_value.bv_len = 0;
347 new->ldctl_value.bv_val = NULL;
350 new->ldctl_iscritical = c->ldctl_iscritical;