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_pvt_ber_remaining(ber);
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 );
144 tag = ber_next_element( ber, &len, opaque ) )
147 LDAPControl **tctrls;
149 tctrl = calloc( 1, sizeof(LDAPControl) );
151 /* allocate pointer space for current controls (nctrls)
152 * + this control + extra NULL
154 tctrls = (tctrl == NULL) ? NULL :
155 realloc(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
157 if( tctrls == NULL ) {
158 /* one of the above allocation failed */
160 if( tctrl != NULL ) {
164 ldap_controls_free(*ctrls);
167 return LDAP_NO_MEMORY;
171 tctrls[nctrls++] = tctrl;
172 tctrls[nctrls] = NULL;
174 tag = ber_scanf( ber, "{a", &tctrl->ldctl_oid );
176 if( tag != LBER_ERROR ) {
177 tag = ber_peek_tag( ber, &len );
180 if( tag == LBER_BOOLEAN ) {
181 tag = ber_scanf( ber, "b", &tctrl->ldctl_iscritical );
184 if( tag != LBER_ERROR ) {
185 tag = ber_peek_tag( ber, &len );
188 if( tag == LBER_OCTETSTRING ) {
189 tag = ber_scanf( ber, "o", &tctrl->ldctl_value );
192 tctrl->ldctl_value.bv_val = NULL;
195 if( tag == LBER_ERROR ) {
197 ldap_controls_free( tctrls );
198 return LDAP_DECODING_ERROR;
211 ldap_control_free( LDAPControl *c )
214 if( c->ldctl_oid != NULL) {
215 free( c->ldctl_oid );
218 if( c->ldctl_value.bv_val != NULL ) {
219 free( c->ldctl_value.bv_val );
227 * Free an array of LDAPControl's
230 ldap_controls_free( LDAPControl **controls )
232 if ( controls != NULL ) {
235 for(c = *controls; c != NULL; c++) {
236 ldap_control_free( c );
244 * Duplicate an array of LDAPControl
246 LDAPControl **ldap_controls_dup( const LDAPControl **controls )
251 if ( controls == NULL ) {
255 /* count the controls */
256 for(i=0; controls[i] != NULL; i++) /* empty */ ;
259 /* no controls to duplicate */
263 new = (LDAPControl **) malloc( i * sizeof(LDAPControl *) );
266 /* memory allocation failure */
270 /* duplicate the controls */
271 for(i=0; controls[i] != NULL; i++) {
272 new[i] = ldap_control_dup( controls[i] );
274 if( new[i] == NULL ) {
275 ldap_controls_free( new );
286 * Duplicate a LDAPControl
288 LDAPControl *ldap_control_dup( const LDAPControl *c )
296 new = (LDAPControl *) malloc( sizeof(LDAPControl) );
302 if( c->ldctl_oid != NULL ) {
303 new->ldctl_oid = strdup( c->ldctl_oid );
305 if(new->ldctl_oid == NULL) {
311 new->ldctl_oid = NULL;
314 if( c->ldctl_value.bv_len > 0 ) {
315 new->ldctl_value.bv_val = (char *) malloc( c->ldctl_value.bv_len );
317 if(new->ldctl_value.bv_val == NULL) {
318 if(new->ldctl_oid != NULL) {
319 free( new->ldctl_oid );
325 SAFEMEMCPY( new->ldctl_value.bv_val, c->ldctl_value.bv_val,
326 c->ldctl_value.bv_len );
328 new->ldctl_value.bv_len = c->ldctl_value.bv_len;
331 new->ldctl_value.bv_len = 0;
332 new->ldctl_value.bv_val = NULL;
335 new->ldctl_iscritical = c->ldctl_iscritical;