]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/controls.c
Provide build support for sasl.c.
[openldap] / libraries / libldap / controls.c
index 9f049c178de31a63466591ce8056f8f6d571ff8d..b609b7bc5660cd0c180fe2df01663248bda74c69 100644 (file)
@@ -2,13 +2,21 @@
  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
-/*
- * LDAP controls
+
+/* LDAPv3 Controls (RFC2251)
+ *
+ *     Controls ::= SEQUENCE OF Control  
+ *
+ *     Control ::= SEQUENCE { 
+ *             controlType             LDAPOID,
+ *             criticality             BOOLEAN DEFAULT FALSE,
+ *             controlValue    OCTET STRING OPTIONAL
+ *     }
  */
 
 #include "portable.h"
 
-#include <stdlib.h>
+#include <ac/stdlib.h>
 
 #include <ac/time.h>
 #include <ac/string.h>
@@ -54,13 +62,13 @@ int ldap_int_put_controls(
        }
 
        /* Controls are encoded as a sequence of sequences */
-       if( ber_printf( ber, "t{", LDAP_TAG_CONTROLS ) == -1 ) {
+       if( ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                return ld->ld_errno;
        }
 
        for( c = ctrls ; *c != NULL; c++ ) {
-               if ( ber_printf( ber, "{s",
+               if ( ber_printf( ber, "{s" /*}*/,
                        (*c)->ldctl_oid ) == -1 )
                {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
@@ -69,7 +77,7 @@ int ldap_int_put_controls(
 
                if( (*c)->ldctl_iscritical /* only if true */
                        &&  ( ber_printf( ber, "b",
-                               (*c)->ldctl_iscritical ) == -1 ) )
+                               (ber_int_t) (*c)->ldctl_iscritical ) == -1 ) )
                {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
                        return ld->ld_errno;
@@ -84,14 +92,14 @@ int ldap_int_put_controls(
                }
 
 
-               if( ber_printf( ber, "}" ) == -1 ) {
+               if( ber_printf( ber, /*{*/"}" ) == -1 ) {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
                        return ld->ld_errno;
                }
        }
 
 
-       if( ber_printf( ber, "}" ) == -1 ) {
+       if( ber_printf( ber, /*{*/"}" ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                return ld->ld_errno;
        }
@@ -99,12 +107,13 @@ int ldap_int_put_controls(
        return LDAP_SUCCESS;
 }
 
-int ldap_int_get_controls LDAP_P((
+int ldap_int_get_controls(
        BerElement *ber,
-       LDAPControl ***ctrls ))
+       LDAPControl ***ctrls )
 {
        int nctrls;
-       unsigned long tag, len;
+       ber_tag_t tag;
+       ber_len_t len;
        char *opaque;
 
        assert( ber != NULL );
@@ -131,7 +140,7 @@ int ldap_int_get_controls LDAP_P((
 
        /* set through each element */
        nctrls = 0;
-       *ctrls = malloc( 1 * sizeof(LDAPControl *) );
+       *ctrls = LDAP_MALLOC( 1 * sizeof(LDAPControl *) );
 
        if( *ctrls == NULL ) {
                return LDAP_NO_MEMORY;
@@ -146,19 +155,19 @@ int ldap_int_get_controls LDAP_P((
                LDAPControl *tctrl;
                LDAPControl **tctrls;
 
-               tctrl = calloc( 1, sizeof(LDAPControl) );
+               tctrl = LDAP_CALLOC( 1, sizeof(LDAPControl) );
 
                /* allocate pointer space for current controls (nctrls)
                 * + this control + extra NULL
                 */
                tctrls = (tctrl == NULL) ? NULL :
-                       realloc(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
+                       LDAP_REALLOC(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
 
                if( tctrls == NULL ) {
                        /* one of the above allocation failed */
 
                        if( tctrl != NULL ) {
-                               free( tctrl );
+                               LDAP_FREE( tctrl );
                        }
 
                        ldap_controls_free(*ctrls);
@@ -171,14 +180,16 @@ int ldap_int_get_controls LDAP_P((
                tctrls[nctrls++] = tctrl;
                tctrls[nctrls] = NULL;
 
-               tag = ber_scanf( ber, "{a", &tctrl->ldctl_oid );
+               tag = ber_scanf( ber, "{a" /*}*/, &tctrl->ldctl_oid );
 
                if( tag != LBER_ERROR ) {
                        tag = ber_peek_tag( ber, &len );
                }
 
                if( tag == LBER_BOOLEAN ) {
-                       tag = ber_scanf( ber, "b", &tctrl->ldctl_iscritical );
+                       ber_int_t crit;
+                       tag = ber_scanf( ber, "b", &crit );
+                       tctrl->ldctl_iscritical = crit ? (char) 0 : (char) ~0;
                }
 
                if( tag != LBER_ERROR ) {
@@ -210,16 +221,18 @@ int ldap_int_get_controls LDAP_P((
 void
 ldap_control_free( LDAPControl *c )
 {
+       assert( c != NULL );
+
        if ( c != NULL ) {
                if( c->ldctl_oid != NULL) {
-                       free( c->ldctl_oid );
+                       LDAP_FREE( c->ldctl_oid );
                }
 
                if( c->ldctl_value.bv_val != NULL ) {
-                       free( c->ldctl_value.bv_val );
+                       LDAP_FREE( c->ldctl_value.bv_val );
                }
 
-               free( c );
+               LDAP_FREE( c );
        }
 }
 
@@ -229,6 +242,8 @@ ldap_control_free( LDAPControl *c )
 void
 ldap_controls_free( LDAPControl **controls )
 {
+       assert( controls != NULL );
+
        if ( controls != NULL ) {
                LDAPControl *c;
 
@@ -236,14 +251,14 @@ ldap_controls_free( LDAPControl **controls )
                        ldap_control_free( c );
                }
 
-               free( controls );
+               LDAP_FREE( controls );
        }
 }
 
 /*
  * Duplicate an array of LDAPControl
  */
-LDAPControl **ldap_controls_dup( const LDAPControl **controls )
+LDAPControl **ldap_controls_dup( LDAPControl **controls )
 {
        LDAPControl **new;
        int i;
@@ -260,7 +275,7 @@ LDAPControl **ldap_controls_dup( const LDAPControl **controls )
                return NULL;
        }
 
-       new = (LDAPControl **) malloc( i * sizeof(LDAPControl *) );
+       new = (LDAPControl **) LDAP_MALLOC( i * sizeof(LDAPControl *) );
 
        if( new == NULL ) {
                /* memory allocation failure */
@@ -285,7 +300,7 @@ LDAPControl **ldap_controls_dup( const LDAPControl **controls )
 /*
  * Duplicate a LDAPControl
  */
-LDAPControl *ldap_control_dup( const LDAPControl *c )
+LDAPControl *ldap_control_dup( LDAPControl *c )
 {
        LDAPControl *new;
 
@@ -293,17 +308,17 @@ LDAPControl *ldap_control_dup( const LDAPControl *c )
                return NULL;
        }
 
-       new = (LDAPControl *) malloc( sizeof(LDAPControl) );
+       new = (LDAPControl *) LDAP_MALLOC( sizeof(LDAPControl) );
 
        if( new == NULL ) {
                return NULL;
        }
 
        if( c->ldctl_oid != NULL ) {
-               new->ldctl_oid = strdup( c->ldctl_oid );
+               new->ldctl_oid = LDAP_STRDUP( c->ldctl_oid );
 
                if(new->ldctl_oid == NULL) {
-                       free( new );
+                       LDAP_FREE( new );
                        return NULL;
                }
 
@@ -312,13 +327,13 @@ LDAPControl *ldap_control_dup( const LDAPControl *c )
        }
 
        if( c->ldctl_value.bv_len > 0 ) {
-               new->ldctl_value.bv_val = (char *) malloc( c->ldctl_value.bv_len );
+               new->ldctl_value.bv_val = (char *) LDAP_MALLOC( c->ldctl_value.bv_len );
 
                if(new->ldctl_value.bv_val == NULL) {
                        if(new->ldctl_oid != NULL) {
-                               free( new->ldctl_oid );
+                               LDAP_FREE( new->ldctl_oid );
                        }
-                       free( new );
+                       LDAP_FREE( new );
                        return NULL;
                }